__init__, __slot__ and **keywordargs

Peter Otten __peter__ at web.de
Fri Jan 9 05:08:08 EST 2004


Zunbeltz Izaola wrote:

> I'm playing with __slot__ (new class) and the __init__ method of my
> class. I want the arguments of __init__ to be keywords and that the
> method initialize all the attributes in __slot__ to None except those
> in the argument of __init__ (Is this a good practice?). I've the
> following class

You misspelt: in fact it's __slots__, and it will work as expected. As far
as I know slots are an optimization technique that is useful to save some
space, when you have many (that would be millions) of small objects. It's
not meant as a safety belt for the programmer.
In your case, I wouldn't use it, but rather not initialize the attributes
not given as constructor - __init__() - arguments. Also, I would consider
the ability to provide additional arguments, say "Nickname", not a bug but
a feature. Of course you have to change your client code to test for

hasattr(person, "Birthday") 

instead of 

person.Birthday is None

Peter





More information about the Python-list mailing list