new-style classes and len method

T

Thomas Girod

Hi there.

I'm trying to use new-style classes, but there is something i'm
obviously missing

here it is :

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)

#> d = Data(2,2)
#> d.clear()
TypeError: len() of unsized object

off course it was working with :

[...]
def clear(self):
while len(self) > 0: del self[0]
return

So, I guess you can't use "cls" as a replacement for "self". So, what
do I have to use ???
 
T

Thomas Girod

Or maybe I'm mixing up what we call a "classmethod" with what we could
call an "instance method" ?
 
P

Paul McGuire

Thomas Girod said:
Hi there.

I'm trying to use new-style classes, but there is something i'm
obviously missing

here it is :

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)

#> d = Data(2,2)
#> d.clear()
TypeError: len() of unsized object

off course it was working with :

[...]
def clear(self):
while len(self) > 0: del self[0]
return

So, I guess you can't use "cls" as a replacement for "self". So, what
do I have to use ???

Um, why do you think clear() needs to be a classmethod? Isn't it supposed
to work on an instance of Data? If you just remove that "clear =
classmethod(clear)" statement, I think this works as you expect.

(Although your implementation of clear() will work correctly, you might
consider the more efficient "del self[:]" slice operation, instead of your
while loop.)
 
B

Ben C

Hi there.

I'm trying to use new-style classes, but there is something i'm
obviously missing

here it is :

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)

#> d = Data(2,2)
#> d.clear()
TypeError: len() of unsized object

off course it was working with :

[...]
def clear(self):
while len(self) > 0: del self[0]
return

So, I guess you can't use "cls" as a replacement for "self". So, what
do I have to use ???

You can use cls in a classmethod to refer to the class. You can use any
name you want (well so long as it's not a "reserved word" I suppose).
The same is true of self, it's just a param name (quite unlike
JavaScript's "this"...).

But I don't think it makes sense for clear to be a classmethod, since
you presumably want a method that clears instances of lists?

The error is because you're trying to take the len of the class itself,
not the len of an instance of it.

What you had before was OK. Although you don't need to write the loop,
just del self[:] will do, and you can leave out the return statement if
you want.
 
T

Thomas Girod

It's alright I found where my mistake came from. I was misunderstanding
the meaning of "classmethod", thinking of it as an instance method.
 
B

bruno at modulix

Thomas said:
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.
 
B

bruno at modulix

Thomas said:
It's alright I found where my mistake came from. I was misunderstanding
the meaning of "classmethod", thinking of it as an instance method.

Given that 'instance methods' being most of the time attributes of the
class object, such a confusion is not so surprising !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top