Hello Steven,

(1) slots = [] doesn't do anything special. You have misspelled __slots__.
Yes sorry my mistake :-[


(2) Classes don't become read only just because you add __slots__ to them. All you prevent is adding extra attributes, and why would you wish to do that?

I know this is ugly but until now it is the only way (with this side effect) I found to declare Enums class that I _understand_:

*class CategoryType(object):
    """Implements the enumeration and prevent associated newly created
instances to redefine enums value via special class variable '__slots__'
    definition. It makes also, instances have no attributes at all.
    """
    __slots__ = ()

    TRANSISTOR, MOS, BIPOLAR, CAPACITOR, RESISTOR, DIODE, ESD, PAD, \
    COMPARATOR, OPAMP, ADC, DAC, PLL, OTHER = range(14)

    def toString ( self, litteral ):
"""Convert the litteral integer number to its associated string representation."""
        if litteral == self.TRANSISTOR:
          return 'transistor'
        elif litteral == self.MOS:
          return 'mos'
        elif litteral == self.BIPOLAR:*
[...]

I discovered recently that I could configure __setattr__(self, name, value) by raising AttributeError to prevent to set up a new attribute value.
Or use an conditional statement to avoid to define it twice.
If you have good advice for enum (I hope I could understand) you welcome ;-) .

Thus I use __slots__, __dict__ (in __setattr__) and I wanted to about much for __weakref__
Have you read the Fine Manual?

http://docs.python.org/reference/datamodel.html#slots


In fact, I already read that. But I am searching for doc about what is a weak reference.


>>> class K1(object):
...     __slots__ = []  # Don't support weak-refs.
...
>>> class K2(object):
...    __slots__ = '__weakref__'  # Do support weak-refs.
...
>>> import weakref
>>> k1 = K1()
>>> k2 = K2()
>>> weakref.ref(k1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create weak reference to 'K1' object
>>> weakref.ref(k2)
<weakref at 0xb7c6661c; to 'K2' at 0xb7ee762c>

Same thing I don't know what to do with this object weakref :-[ as I don't know its meaning. That was my true question in fact;

Regards

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to