True inconsistency in Python

Terry Reedy tjreedy at udel.edu
Mon Nov 17 23:13:24 EST 2003


"Ron Adam" <radam2 at tampabay.rr.com> wrote in message
news:ivqhrv403or6mm68arqg5793gnaaehjq8e at 4ax.com...
> x = True
> if x:
> ....

since the above snippert is equivalent to

if True:
...

which is equivalent to

...

you must have meant something more imvolved.  But I am having trouble
filling in the missing pieces.

> So I get consistent results for the language and platform I'm using
> now and in the future.  If down the road someone decided to make
True
> = 3, and False = -5,  and they change the language so that any
boolean
> comparisons return 3 and -5 respectively,  my use of True and False
> will still work.

This strikes me as comparable to worrying about being killed by an
asteroid.  Is there some actual historical event that makes it more
plausible to you?

'Someone' would have to be someone with the authority to make such a
change in either the language or a particular implementation thereof.
Such people are usually at least somewhat sane.  If someone did make
such a lunatic change, I suspect that it would not be the only one,
and one would have to be a lunatic to keep using that
language/implementation for serious work.  Unless the language were
locked up and proprietary, users could reject such a change anyway.
(On the other hand, exposure to lesser lunacies *is* one of the risks
of using proprietary unclonible systems.)

...
> It looks to me that 'True' in python is a combination of the boolean
> binary logic of 1 or 0, and as an "exists" test of 0 or not 0.

I do not understand this.  If you have a reference to 0, then 0
exists.  If you do not, then the 'existence' or not of an int with
value 0 is irrelevant.

> If python had an exists operator, you could do.

I think you are confusing names and objects.  You can only apply an
operator to objects and only to objects that exist.

> if  x exists:
> 'do something'

Do you mean
if bound_to_something('x'): <do something>
?

Normally, in the absence of error, one only uses names that one has
defined (bound to somethingj).  So you must be worried about the
following scenario:

if a: x=1
<more code>
if exists(x): <do something with x>

In non-module script only:
import __main__ as mm
x = 1
hasattr(mm, 'x') # True
hasattr(mm, 'y') # False

Anywhere: use try ... except NameError

Or, don't do that (have conditionally defined name).  Instead,

x=None
if a: x = something_not_None()
<more code>
if x != None: <do something with x>

In other words, name != None is often idiom for exists('name')!

> This could serve two options  also...  does the object exist?

Again, Python only operates on objects that do exist.

...
> With pythons dynamic variables,  I think an exists function would be
> useful to check if an object exists.

Same comment.

Terry J. Reedy






More information about the Python-list mailing list