PEP 285: Adding a bool type

Alex Martelli aleax at aleax.it
Mon Apr 8 17:48:20 EDT 2002


Bengt Richter wrote:
        ...
> Well, I guess that depends on how you define "assignment" in Python.

I don't have to, as the Language Reference sees to that:
http://www.python.org/dev/doc/devel/ref/assignment.html

> ISTM there really is no such thing, in the sense of updating a named
> storage place with new content, 

"Assignment statements are used to (re)bind names to values and to modify 
attributes or items of mutable objects" -- I guess one COULD at the
limit call an attribute "a named storage place", but it would not be a very
fruitful thing in Python.

> expression ;-) I'm not sure what that side effect should be called
> if not assignment, since it caused the identical binding that
> a='bee' would in that context.

It's a serious logical fallacy to reason "assignment statements may
(among other things) rebind names, therefore what rebinds a name
is an assignment".  Many bindings occur other than by assignment.
Would you call a for statement, a def statement, a class statement,
or an import statement "assignments", because they also rebind
names?!

> If it binds like a duck... ;-)

Should I try to display my imitatory abilities by quacking like a duck,
that does not justify your shooting me even if you have a hunting
license and ducks are in season.

> My criterion for judging whether it was an assignment was whether
> it accomplished the same name re/binding as a normal assignment,
> and I think it did, in the demonstrated context. So IMO Aahz's
> quote doesn't quite remain perfectly right ;-)

In many restricted contexts it's long been perfectly feasible to
perform a binding (and nothing but that) within an expression.
Your example was horrid -- it works "by accident" in a given
implementation, in a certain restricted context, because a
undefined behavior happens to be that way in said specific
implementation.  One can of course do much better than that:

        [x for x in (y,)][0]

is an expression perfectly valid in all contexts which binds or
rebinds name x to the same value as name y is bound to and
also has that value as its value-as-expression.  It doesn't
take all that much Python knowledge to build it, either.

So, whatever you could do in a language that allowed assignments
in expressions you could do in Python via that hard-to-read way, e.g.:

while [x for x in (getnext(23),)][0]:
    frobnicate(x)

is the Python (but NOT Pythonic) equivalent of C's

while(x=getnext(23))
    frobnicate(x);

That doesn't affect the "perfectly right" status of Aahz's quote, either.


Alex




More information about the Python-list mailing list