[Tutor] Using new style classes and __slots__

Kent Johnson kent37 at tds.net
Sat Sep 24 16:20:42 CEST 2005


mailing list wrote:
> Hi Kent,
> 
> 
> 
>> >>> class foo(object):
>> ...   __slots__ = ['x', 'y']
>> ...
>> >>> f=foo()
>> >>> f.x=1
>> >>> f.y=2
>> >>> f.z=3
>>Traceback (most recent call last):
>>  File "<stdin>", line 1, in ?
>>AttributeError: 'foo' object has no attribute 'z'
>>
>>Take a look at
>>http://www.python.org/2.2.3/descrintro.html (search for __slots__)
> 
> 
> Thanks for that. I was tearing my hair out trying to figure out why
> despite the usage of __slots__ my instances still had a __dict__, and
> then I saw the subclass of object, and sure enough... I'm guessing
> that's how you get new style classes.

Yes, exactly. To be precise: A class is a new-style class if its metaclass is type 'type' or inherits from type. If you inherit from a new-style class, your class will have the same metaclass as the base class and thus be a new-style class. The simplest way to do this is to inherit from object.
 
> Quite like what I'm seeing, __slots__ and that property() thing will
> allow me to remove all my __setattr__ and __getattr__ methods, which
> tend to make things crawl a wee bit.

That's exactly what property() is for.
 
> I was almost considering writing setters/getters to get around the
> magic methods, but I grew to dislike them after having used Java, I
> got annoyed at having to write setters/getters for every public
> variable. Of course, I was using Notepad to write the Java programmes,
> and then Eclipse helped me  realise why Java programmers value IDE's
> so much, it's that automatic generation of set/gets...

One of the cool things about properties is that you can start with simple attributes and change them later to have behaviour without having to change the client code or use getters and setters all the time.
 
> Out of curiosity, if you're using __slots__ with new style, you no
> longer have __dict__. So, where does it stick data?

My understanding is that there are actually 'slots' allocated in the object to hold the references to the attribute data, kind of like a C struct. I'm not sure about this though.
 
> I was using 'self.__dict__["_created"] = False' to set a flag at the
> start of __init__ to stop __setattr__ picking up the attributes being
> set from parsed data (and running all my type checks, etc.),  but I
> won't need that anymore. I could almost hug property(). :)

You might want to learn more about the whole property mechanism then. property() is actually a bit of sugar over a deeper mechanism. Also there is an interesting idiom for creating properties using @apply (in Python 2.4) - look for Benji York's comment in this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410698

> 
> All in all, this has been an interesting tour around the inner
> workings of a class in Python.
> Give it another five years, I might be able to read one of those
> metaclass explanations and understand it.

Me too!

Kent
> 
> 
> Regards,
> 
> Liam Clarke
> 
> PS Just for my edification, is there a builtin to determine how much
> memory is allocated to an object?
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 



More information about the Tutor mailing list