[Tutor] Misc question about scoping

Steven D'Aprano steve at pearwood.info
Fri Jun 4 02:13:28 CEST 2010


On Fri, 4 Jun 2010 03:10:41 am Alan Gauld wrote:
> "Tino Dai" <oberoc at gmail.com> wrote
>
> >    Is there a way to express this:
> >    isThumbnail = False
> >    if size == "thumbnail":
> >        isThumbnail = True
> >
> >     like this:
> >     [ isThumbnail = True if size == "thumbnail" isThumbnail =
> > False ]
>
> Bob showed one way, you could also do:
>
> isThumbnail = True if size == "thumbnail" else False

That is technically correct, you could do that. That's a good example of 
the syntax of the `if` expression, but it's a bad example of where to 
use it:

(1) it only works in Python 2.5 or better; and 

(2) experienced Python programmers will laugh at you :)

with all due respect to Alan who suggested it. It really is an 
unnecessarily complicated and verbose way of doing a simple assignment, 
nearly as bad as writing this:

if len(mystring) == 0:
    n = 0
else:
    n = len(mystring)


instead of just

n = len(mystring)


-- 
Steven D'Aprano


More information about the Tutor mailing list