Does type-class unification provide static classes, a la C++ ?

Russell Turpin russell_turpin at hotmail.com
Wed Sep 5 12:29:47 EDT 2001


If I correctly read Guido's description, and correctly
understand Alex's comments, future Python will support
static classes, much like those of C++, without the 
overhead of a __dict__:

    class TwoAttrsOnly(object):
        __slots__ = ["one", "two"]

        # Define methods

        <etc>

Guido writes:

  "The __slots__ declaration takes a list of instance 
  variables, and reserves space for exactly these in 
  the instance. When __slots__ is used, other instance 
  variables cannot be assigned to .."

I assume this means also that the instance cannot be 
dynamically assigned methods:

    d2 = TwoAttrsOnly()
    d2.fauxMeth = lambda x=d2: someFunc(x)  

In classic Python, the last line above creates something
that acts a lot like a new method for d2. Given the use
of __slots__, in new Python, I would expect that line to
generate an attribute error. Is that correct? 

In other words, __slots__ says "these values complement
the class __dict__, and since I'm defined, there is no
instance __dict__." But even with __slots__ defined, I 
can do the following:

    d2.one = lambda x=d2: someFunc(x)

This uses the first slot for a new "faux" method rather 
than a data attribute. Is this all cool? (Not as recommended
programming practice, of course. I'm just trying to 
understand the coming type model.)

Russell



More information about the Python-list mailing list