tkFileDialog question

r rt8396 at gmail.com
Sun Nov 15 22:27:38 EST 2009


On Nov 15, 8:56 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Fri, 13 Nov 2009 11:32:37 -0300, Matt Mitchell  
> <mmitch... at transparent.com> escribió:
>
> > answer = tkFileDialog.askdirectory()
>
> > if answer is not '':
> >    #do stuff
>
> Although it "reads well", this is *wrong*. You want != here, not the `is  
> not` operator.
>
> if answer != '': ...
>
> If you want to compare the *values* of two objects, to see if they are  
> equal or not, use == or !=.
> If you want to compare *identity*, to see if two results refer to the very  
> same object or not, use `is` or `is not`

Actually "Gabe" your right and wrong.. the pythonic way to check for
True/False is..

path = tkFileDialog.askwhatever()

if path:
   #do something here

NOT

if path != '':
   #do something here

Both work equally but the first is more Pythonic! UNLESS you are
specifically testing for anything but the empty string that is. In
that case your way *is* correct. But that is not the case with a file
dialog return value. Yes the dialog returns either an empty string OR
a valid path but True False is all that matters here when deciding
whether to act on that path -- or not act on it. NOT whether or not it
is an empty string.



More information about the Python-list mailing list