Python Newbie

Michael Torrie torriem at gmail.com
Sat Feb 23 13:58:39 EST 2013


On 02/21/2013 04:34 PM, piterrr.dolinski at gmail.com 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 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.

As others have said, I don't see why this should be confusing.  It's a
common idiom that happens in C# or C all the time:

if (!some_state)
{
	//blah
}

There is absolutely no ambiguity here.  Every C# and C programmer knows
exactly what this means.  As others have said, it's the equivalent of
saying,  ((bool)(expression) != True). Just in a cleaner way and in a
way that echos how most programmers actually think.  And in python,
dropping the unnecessary comparisons actually gives you a speed up too.

In python there is some ambiguity as to what constitutes a boolean truth
or falsehood value for some types.  For example, an empty list ([]) is
false, as is an empty dictionary ({}), and a number is 0.  And in some
cases (default arguments, for example), using a "is None" comparison is
required. For example:

def some_function( a = None):
    # None is used above as the default because lists are
    # mutable, and if a list were placed up there it would
    # affect all subsequent calls to some_function()

    if a is None:
        a = [ 'default', 'values', 'here']

    for x in a:
        print x

some_func()
some_func(1,2,3)

> 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.

As long as you are trying to write C# code in Python, you'll be very
frustrated.  Takes some time to learn about what it means to write
"pythonic" code.  You'll be well-served and you will start to enjoy the
language more.  Ditch the excess parenthesis.  They don't make things
clearer necessary and they really confuse what is a statement with what
is an expression (a pet peeve of mine with C).

Finally take some time to learn about how Python works from a language
theory point of view.  One interesting thing you'll learn is that python
actually has no variables.  This is a powerful concept, but can get you
in trouble sometimes when you're not aware of this fact.



More information about the Python-list mailing list