Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Newbie

Reply
Thread Tools

Python Newbie

 
 
piterrr.dolinski@gmail.com
Guest
Posts: n/a
 
      02-21-2013
Hi Chris,

Thanks for this. Regarding ambiguity, you will never find me write ambiguous code. I don't sabotage my own work. But the reality is that in addition to writing my own code, I have to maintain existing. I find it incredibly confusing then I see a statement along the lines of "if not something" - haveto study the code in detail to see what it is testing.

I could show more examples of what I find confusing in existing code, but Idon't intent to troll. I'm just trying to understand the language as it is.. I will see how it goes.

Pete
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      02-21-2013
On 21/02/2013 23:34, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi Chris,
>
> Thanks for this. Regarding ambiguity, you will never find me write ambiguous code. I don't sabotage my own work. But the reality is that in addition to writing my own code, I have to maintain existing. I find it incredibly confusing then I see a statement along the lines of "if not something" - have to study the code in detail to see what it is testing.
>
> I could show more examples of what I find confusing in existing code, but I don't intent to troll. I'm just trying to understand the language as it is. I will see how it goes.
>
> Pete
>


Perhaps look at code in the standard library to get a feel for things?
This should help you overcome OCD, i.e. Obsessive C Disorder

--
Cheers.

Mark Lawrence

 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      02-21-2013
On Thu, Feb 21, 2013 at 3:40 PM, <(E-Mail Removed)> wrote:
> I am nervous about using variables "out of the blue", without having to declare them. For example, when I write "i = 0" it is perfectly OK to Python without 'i' being declared earlier. How do I know that I haven't used this variable earlier and I am unintentionally overwriting the value? I find I constantly have to use the search facility in the editor, which is not fun.


If you need to search for variable names to see if you're overwriting
something, then your functions are too large and should probably be
refactored, or you're abusing globals, or possibly you just haven't
fully understood Python's scoping rules.

> You see, Javascript, for one, behaves the same way as Python (no variabledeclaration) but JS has curly braces and you know the variable you have just used is limited in scope to the code within the { }. With Python, you have to search the whole file.


Er, this doesn't sound right at all. Javascript does have variable
declarations, using the "var" keyword. Within a function, a declared
variable has local scope, but an undeclared variable has global scope.
Despite having curly braces, Javascript does not have block scopes
like C# does. More details can be found here:

http://stackoverflow.com/questions/5...variable-scope

In Python, on the other hand, an undeclared variable in a function is
local by default (unless it is never assigned to). To me, this makes
Python win out over Javascript because you're never going to
accidentally create a global variable just by failing to declare the
variable in the function where you use it.
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-22-2013
On Fri, Feb 22, 2013 at 10:34 AM, <(E-Mail Removed)> wrote:
> Thanks for this. Regarding ambiguity, you will never find me write ambiguous code. I don't sabotage my own work. But the reality is that in additionto writing my own code, I have to maintain existing. I find it incredibly confusing then I see a statement along the lines of "if not something" - have to study the code in detail to see what it is testing.


It's testing something - specifically, whether or not the object wants
to "feel true" or "feel false". It's up to the object to define what
that means.

Most of the time, it's exactly what you want. When it isn't, you can
put an exact comparison.

ChrisA
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      02-22-2013
On Thu, 21 Feb 2013 13:26:41 -0800 (PST), Piterrr
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:

> For example, you open a source file in different editors and the indentation levels change even though i only have spaces, no tabs (compare Windows Notepad and Notepad++).


Is one using a proportional spaced font while the other is using a
mono spaced font?

> yntax. For example, when I write "if (myVariable != 0):" then this is OK but "for (i in intAry):" results in syntax error. Apparently Python has problems with my use of parentheses. How retarded. I


No -- (i in intAry) is returning a single boolean result (is the
value of "i" IN the value of "intAry"), so "for (i in intAry)" becomes
the equivalent of "for True" or "for False" depending on the result of
the "in" operator.
--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      02-22-2013
On 02/21/2013 04:26 PM, Piterrr wrote:
> Hi folks.
> I am a long time C sharp dev, just learning Python now due to job
> requirements. My initial impression is that Python has got to be the
> most ambiguous and vague language I have seen to date. I have major
> issues with the fact that white space matters. How do you deal with
> this? For example, you open a source file in different editors and the
> indentation levels change even though i only have spaces, no tabs
> (compare Windows Notepad and Notepad++). Which editor do you trust? In
> addition, code is difficult to read because you cannot lay it out in
> easily discernable blocks. For example, I always tend to indent a full
> 'if' statement block so that it is easier to see where the if block
> starts and ends. Can't do that in Python. What is even more
> frustrating is that Python is inconsistent with its syntax. For
> example, when I write "if (myVariable != 0):" then this is OK but "for
> (i in intAry):" results in syntax error. Apparently Python has
> problems with my use of parentheses. How reta
> rded. I
> think I will rather find another job than eat my nerves with Python.
> Any comments on this before I quit my job?



I think you need to distinguish between your own stylistic preference
and considerations of writing in a style that is clear and readable for
other developers who will be working with your code.

For example:

....

if condition {
...
}

....


Looks very unclear and confusing to me. Whether it's C# or ruby or
anything else, most devs don't indent like that; and that raises a
question - was an outer block accidentally deleted? How do I know if it's
your personal style or a mistake?

Even if I know it is intentional, it's not clear why an if block is
treated in a special way... what about for loops, try/except blocks,
etc? To me, if operator being at the same indentation indicates that it
is executed after the statements that precede it and before the ones
that follow it -- can't get any clearer than that!

Things like:

if (condition == value)

look like unnecessary visual noise to me. Parens are useful and are
meant to group conditions when their number becomes unwieldy and hard to
read and to override operator order of precendence (or rather to avoid
having to remember it):

if (condition == value * modifier) or (condition2 == default_value):

In regard to "truthy" evaluation -- it makes more sense if variables
have good names, as they should; for example:

if mylist: .. do something with the list ..

if mystring: .. process string ..

if my_things_count:
.. there were some things, process them ..
else:
.. there were no things! ..


In all of these cases, my intent is to ask if the value is empty or not,
or blank, or holds some non-false contents. It may also be a custom
object which is responsible for telling me, in a standardized way, if
it's blank/False or not. In 99.8% of cases, do I care if mystring is
None or '' or False? Of course not! Do I care if mylist is None or []?
Almost certainly not.

And the nice thing is, in the rare case that you do care, you'll
write 'if mylist is None:' -- and that alerts the reader to the fact
that mylist's specific value is important.

Reducing unnecessary visual noise in the code is a worthy goal -- it
lets your eyes focus more on the logic of the program.

HTH, -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

Language is like money, without which specific relative values may well
exist and be felt, but cannot be reduced to a common denominator.
George Santayana

 
Reply With Quote
 
rusi
Guest
Posts: n/a
 
      02-22-2013
On Feb 22, 3:40*am, (E-Mail Removed) wrote:
> Thanks to all for quick relies.
>
> Chris, you are (almost) spot on with the if blocks indentation. This is what I do, and it has served me well for 15 years.
>
> code
> code
>
> * *if (some condition)
> * *{
> * * * code
> * * * code
> * *}
>
> code
> code
>
> This is what I call code clarity. With Python, I am having to do this
>
> code
> code
>
> ##############################
>
> if (some condition):
> * code
> * code
>
> ##############################
>
> code
> code
>
> It does the job, but is not ideal.
>
> I am nervous about using variables "out of the blue", without having to declare them. For example, when I write "i = 0" it is perfectly OK to Python without 'i' being declared earlier. How do I know that I haven't used this variable earlier and I am unintentionally overwriting the value? I find I constantly have to use the search facility in the editor, which is not fun.
>
> You see, Javascript, for one, behaves the same way as Python (no variabledeclaration) but JS has curly braces and you know the variable you have just used is limited in scope to the code within the { }. With Python, you have to search the whole file.
>
> Thanks to Chris, Ian and Dave for explaining the () issue around if and for statement. I don't agree with this, but I understand your points. The reason why I like parentheses is because they help with code clarity. I am obsessed with this. After all, there is a reason why so many languages have required them for several decades.
>
> What about Python's ambiguity?
> For example, in C you would write
>
> if (myVar != 0)
> * do something
>
> in Python, this is legal
>
> if (not myVar):
> * do something


I have seen any amount of C code where:
if (x != 0) { ...

is shortened to

if (x) { ...

Python is similar (and many of us dont like this feature).
Just know it as a feature and get on with it. Python has less
features/gotchas/bugs than most other languages.

As for indentation: This is more a philosophy than a technology
question. Its called DRY (Dont Repeat Yourself) In most languages
(other than python and haskell) you use {} or some such for the
compiler and indentation for the human reader. Saying something
exactly once is not just a space/time saver. Its also a error saver.

> In the mean time, thanks to most of you for encouraging me to give Pythona chance. I will do
> my best to like it, w/o prejudice.


The mathematician Paul Halmos was once told by a bewildered student:
"I just dont understand mathematics!"
Halmos replied: My boy you dont understand mathematics. You just get
used to it.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-22-2013
On Thu, 21 Feb 2013 14:40:37 -0800, piterrr.dolinski wrote:

> With Python, I am having to do this
>
> code
> code
>
> ##############################
>
> if (some condition):
> code
> code
>
> ##############################



I prefer this:


# WARNING WARNING WARNING IF STATEMENT AHEAD BEWARE BEWARE BEWARE
if something():
do(stuff)
# WHEW IF STATEMENT FINISHED!!! WE SURVIVED, THANK THE GODS!!!


That way I can be sure that people reading it will know it is an "if" and
not a "for" or "while", just in case the actual code wasn't clear enough.



> I am nervous about using variables "out of the blue", without having to
> declare them.


I feel your pain. I'm nervous about declaring variables "out of the
blue", without having to declare that I'm about to declare them. I live
in hope that some day I will find a language that lets me write:

FORWARD VARS:
int x;
double y;
str s;

VARS:
int x;
float y;
str s;


and have the compiler catch my error in declaring y as a float instead of
a double. Then I will be one step closer to my dream of not having to
understand the code I write.


> For example, when I write "i = 0" it is perfectly OK to
> Python without 'i' being declared earlier. How do I know that I haven't
> used this variable earlier and I am unintentionally overwriting the
> value? I find I constantly have to use the search facility in the
> editor, which is not fun.


Some people suggest that functions should be small enough to read over in
a glance, or at least a couple of glances. They even say that variable
names should be meaningful, so you can tell whether a variable has been
used from context. I say, fi to that! Programming should be a challenge!
It should be exciting! Which is why I never use functions, and always use
the same single-letter variable with a cryptographic hash to distinguish
them.

i048b8497cf86dab9dade2ce6beddf13a = []
i048b6497cd85d9b9da2a3ce6bdedf167 = 42


*wink*


I'm not going to apologise for taking the ****, although I do hope you
will take it in the spirit it is intended: constructive criticism rather
than abuse. The reality is, these "problems" you are running into are not
problems in practice, and the "solutions" you are used to, like braces,
are visually noisy and not terribly effective. You would probably think
differently of { } if they were spelled "BEGIN" and "END" like in Pascal.

Braces have one, and only one, advantage: if you pass code through a
noisy environment that deletes whitespace at will, you can recover the
structure of the code from the braces. Python does not have that
advantage, and sometimes it is a pain, e.g. when people email code using
a broken mail client that deletes spaces.

But then, if you passed C code through something that deleted the braces,
you'd be royally screwed too. The solution to that is to stop having
tools that think that whitespace isn't significant. Of course it is
significant, it is significant to the human reader, who is about a
hundred billion trillion times more important than the compiler.



--
Steven
 
Reply With Quote
 
Steve Simmons
Guest
Posts: n/a
 
      02-22-2013
Dear Mr D'Aprano,

I thank you for your post but I must complain in the strongest possible
terms that it was not enclosed in the correct delimeters. It should
have been enclosed in a <humour>... </humour> pair (or
<humor>...</humor> if you are American).

I was drinking coffee at the time I started reading your post and,
entirely due to your negligent lack of proper delimiters, much of my
coffee is now irretrievably resident in my keyboard. My lawyers will be
in touch...


On 22/02/2013 11:26, Steven D'Aprano wrote:
> On Thu, 21 Feb 2013 14:40:37 -0800, piterrr.dolinski wrote:
>
>> With Python, I am having to do this
>>
>> code
>> code
>>
>> ##############################
>>
>> if (some condition):
>> code
>> code
>>
>> ##############################

>
> I prefer this:
>
>
> # WARNING WARNING WARNING IF STATEMENT AHEAD BEWARE BEWARE BEWARE
> if something():
> do(stuff)
> # WHEW IF STATEMENT FINISHED!!! WE SURVIVED, THANK THE GODS!!!
>
>
> That way I can be sure that people reading it will know it is an "if" and
> not a "for" or "while", just in case the actual code wasn't clear enough.
>
>
>
>> I am nervous about using variables "out of the blue", without having to
>> declare them.

> I feel your pain. I'm nervous about declaring variables "out of the
> blue", without having to declare that I'm about to declare them. I live
> in hope that some day I will find a language that lets me write:
>
> FORWARD VARS:
> int x;
> double y;
> str s;
>
> VARS:
> int x;
> float y;
> str s;
>
>
> and have the compiler catch my error in declaring y as a float instead of
> a double. Then I will be one step closer to my dream of not having to
> understand the code I write.
>
>
>> For example, when I write "i = 0" it is perfectly OK to
>> Python without 'i' being declared earlier. How do I know that I haven't
>> used this variable earlier and I am unintentionally overwriting the
>> value? I find I constantly have to use the search facility in the
>> editor, which is not fun.

> Some people suggest that functions should be small enough to read over in
> a glance, or at least a couple of glances. They even say that variable
> names should be meaningful, so you can tell whether a variable has been
> used from context. I say, fi to that! Programming should be a challenge!
> It should be exciting! Which is why I never use functions, and always use
> the same single-letter variable with a cryptographic hash to distinguish
> them.
>
> i048b8497cf86dab9dade2ce6beddf13a = []
> i048b6497cd85d9b9da2a3ce6bdedf167 = 42
>
>
> *wink*
>
>
> I'm not going to apologise for taking the ****, although I do hope you
> will take it in the spirit it is intended: constructive criticism rather
> than abuse. The reality is, these "problems" you are running into are not
> problems in practice, and the "solutions" you are used to, like braces,
> are visually noisy and not terribly effective. You would probably think
> differently of { } if they were spelled "BEGIN" and "END" like in Pascal.
>
> Braces have one, and only one, advantage: if you pass code through a
> noisy environment that deletes whitespace at will, you can recover the
> structure of the code from the braces. Python does not have that
> advantage, and sometimes it is a pain, e.g. when people email code using
> a broken mail client that deletes spaces.
>
> But then, if you passed C code through something that deleted the braces,
> you'd be royally screwed too. The solution to that is to stop having
> tools that think that whitespace isn't significant. Of course it is
> significant, it is significant to the human reader, who is about a
> hundred billion trillion times more important than the compiler.
>
>
>


 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-22-2013
On Fri, Feb 22, 2013 at 10:05 PM, Steve Simmons <(E-Mail Removed)> wrote:
> I thank you for your post but I must complain in the strongest possible
> terms that it was not enclosed in the correct delimeters. It should have
> been enclosed in a <humour>... </humour> pair (or <humor>...</humor> if you
> are American).


Steve, you clearly do not understand the vital importance of these
tags, which is for the reader, not the writer! Steven should have
enclosed his text in *both* markers, in order that readers familiar
with only one of the two systems would nevertheless be spared the
indignity of having to learn the other.

<signature>
<name>
<.sig>
ChrisA
</signature>
</.sig>
</name>

.... there, have fun unnesting that...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib, winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 12 04-22-2005 12:19 PM
Re: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Jaime Wyant Python 0 04-21-2005 12:49 PM
RE: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 0 04-19-2005 09:27 AM
Python Newbie. Python on PPC, please assist. pythonnewbie Python 5 10-12-2004 05:51 PM
Creating an OLE server document in Python (MFC/OLE/COM/Python newbie) Drew Pihera Python 0 02-04-2004 07:48 PM



Advertisments