new-style classes and len method

bruno at modulix onurb at xiludom.gro
Thu Apr 13 12:02:14 EDT 2006


Thomas Girod wrote:
> Or maybe I'm mixing up what we call a "classmethod" with what we could
> call an "instance method" ?
> 
That's what I was about to point out !-)

> class Data(list):
>     __slots__ = ["width", "height", "label"]
> 
>     def __init__(self,width,height,label=None):
>         list.__init__(self)
>         self.width = width
>         self.height = height
>         self.label = label
> 
>     def clear(cls):
>         while len(cls) > 0: del cls[0]
>         return
>     clear = classmethod(clear)

What about this ?

def clear(self):
  del self[:]

As it's name (and signature) implies, a classmethod works on a class,
not on an instance of the class. Here, you are trying to compute the
length of *class* Data.


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list