[Tutor] How to perform variable assignment and

Rich Lovely roadierich at googlemail.com
Sat Oct 3 10:04:07 CEST 2009

2009/10/3 wesley chun <wescpy at gmail.com>:
> On Fri, Oct 2, 2009 at 11:14 PM, Oxymoron <moron.oxy at gmail.com> wrote:
>> Hello,
>> On Sat, Oct 3, 2009 at 3:56 PM, Didar Hossain <didar.hossain at gmail.com>
>> wrote:
>>> homedir = os.environ.get('HOME')
>>> if homedir:
>>>    print "My home directory is %s" % homedir
>>> I do this in Perl -
>>> my $home;
>>> if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; }
>>> Can I do a similar shortcut statement like the above in Python?
>> There are probably various syntactic tricks to achieve the same, however,
>> the main reason you can't do it is that assignment in Python is a statement
>> rather than an expression, i.e. it does not return a value that the if
>> statement can evaluate.
> kamal is correct. you cannot do it in Python because assignments are
> not expressions, and when they are, it leads to problems, i.e., code
> readability, bugs, etc. Python fights hard to prevent those from
> "interrupting your problem-solving," and there's a cost to it --
> hopefully the benefits outweigh the minor costs.
> as far as your solution goes, it is one of the cleanest solution you
> can come up with. however, there is a tiny bug: if the $HOME
> environment variable is *not* set, you will get a KeyError exception.
> one solution is to add a default value to your get() method call so
> that it returns an object with a Boolean False value:
> import os
> homedir = os.environ.get('HOME', '') # or False or None
> if homedir:
>   print "My home directory is %s" % homedir
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>    http://corepython.com
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

This message mentions, but skips over one of the differences in the
mindsets of perl and python.

Perl is designed to "Look Before You Leap", hence the if-statement in
your example.  Python is designed with the mindset that "It's easier
to ask forgivness than permission".  This is commonly known as LBYL
vs. EAFP

So whilst the perl would be
if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; }

the congruent Python would probably would be something like

    home = os.environ['HOME']
except KeyError:
    home = None
    print "My home directory is", home

Admittedly, I don't know what the value of $home would be after
executing the snippet above, but I'm assuming nil or null or whatever
the perl equivalent is.

In a sort of summary:  LBYL means lots of if-statements: Is there a
chance value X won't work in this function, if so, let's not try.
EAFP means lots of exception handling: Let's try X in this function,
and if it goes wrong, we'll deal with it then.

Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.

More information about the Tutor mailing list