Python Newbie

Nick Mellor thebalancepro at gmail.com
Tue Feb 26 04:37:54 CET 2013


Hi Piterr,

It's interesting how strong our habits are, isn't it? It's most likely you've just got a bit of culture shock.

I've used C# quite a bit as well as Python. I like them both.

What I like about Python is how it manages to be clear and terse at the same time.

if (flag==1) {
    code
}

has quite a bit of redundancy in it (C or C#.) In Python:

if flag:
    code

is a good shorthand that matches any non-zero value. Gone are the brackets and the braces and the meaning is crystal clear.

In the middle of a complex piece of code this clarity of syntax is fantastic for anyone reading the code-- so long as they know how Python works.

You may discover as you continue with Python that there is actually quite a lot of freedom about how you use white space. Python doesn't much care *how* much you indent, just that you're consistent within the block:

if flag:
            # do something
            # do something else
# continue coding after the if stmt

is equivalent to

if flag:
  # do something
  # do something else
# continue coding after the if stmt

You're also allowed to break a line inside brackets, braces etc and use any indenting you like:

if flag:
    [o.process(i % 3 + w)
         for i, o, w
             in enumerate(zip(objects, words))
    ]
    # do something else
# this is after the if block

The following is a good thread about "implied line continuation" (i.e. where line breaks and free indentation are allowed inside braces, square brackets or round brackets.)

http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python

See also the Python style guide:

http://www.python.org/dev/peps/pep-0008/

There's also explicit line continuation, rather like Visual Basic:

if c + \
    256 * \
    (d + 256 * e) > 69427:
       # do something
       # do something else
# then leave the if block

Again, the line continuation doesn't mind what indent you use after it.

In this case you could put brackets around the expression and use implied line continuation:

if (c +
    256 *
    (d + 256 * e)) > 69427:
       # do something
       # do something else
# then leave the if block

This is the preferred method according to the Python style guide.

I hope you manage to give Python (and your job) a little longer to charm you
:-)

Cheers,

Nick

On Friday, 22 February 2013 08:26:41 UTC+11, 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 retarded. I think I will rather find another job than eat my nerves with Python.
> 
> Any comments on this before I quit my job?



More information about the Python-list mailing list