__slots__ questions (v2.2.2)

Carl Banks imbosol at vt.edu
Tue Oct 15 19:03:16 EDT 2002


Holden Caulfield wrote:
> 2)
> Also, in python 2.2.2 as in 2.2.1, a variable in the '__slots__' *must*
> still be initialized 'before use'. I do not whether
> it is supposed to work that way. If it is, then it seem very
> counter-intuitive (well, if you are in the new features bandwagon).

It might be counterintuitive (to some), but that's pretty much the
same way Python has dealt with a similar circumstance, namely local
variables.

Try this:

    x = 1
    def f():
        print x
        x = 2
    f()


This should raise an UnboundLocal exception.  Python scans the
function, sees that x was assigned and considers it a local variable
in f, which shadows the global.  But x is not initialized to anything;
it is unbound until it is assigned to.

If Python classes had initialized the slots to None, then I guarantee
you there would be much complaining because of inconsistency with how
local variables work.

As it is, *I* don't find this behavior counterintutitive at all.
Remember the Zen of Python: "Explicit is better than implicit."  Until
you set it, it's nothing (even more nothing than None), and I think
that's how it should be.


-- 
CARL BANKS
http://www.aerojockey.com



More information about the Python-list mailing list