Instance of class "object"

Terry Reedy tjreedy at udel.edu
Fri May 16 20:13:42 EDT 2008


"Ìð¹Ï" <littlesweetmelon at gmail.com> wrote in message 
news:cdb837ea0805160309g6ea51644nf2337bcddc34eb21 at mail.gmail.com...
| > # an efficient 'Pair' class holding two objects
| > class Pair(object):
| >    __slots__ = 'first', 'second'
| >
| > Instances of Pair take up even less room that 2-element tuples
| > because they don't carry the size information in the object.
| >
| > Now, if the object class carried a dict with it, it would be
| > impossible to create a class like 'Pair'.
| >
| Really interesting. When the tuple ('first', 'second') is assigning to
| __slot__, a special operation is done which makes __slot__ pointing
| to a magic structure rather than a normal tuple. Am I right?

Try it.  Run the code and print P.__slots__.
<pause>

Class statements are implemented by calling the metaclass 'type' with 3 
args.  Type.__new__ uses the information in those args.  If the namespace 
arg has a __slots__ member, it does something special based on the strings 
in the tuple, but it leaves the tuple alone. 






More information about the Python-list mailing list