Python Newbie

Mitya Sirenef msirenef at lightbird.net
Fri Feb 22 05:50:48 CET 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




More information about the Python-list mailing list