473,473 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

new-style classes and len method

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 ???

Apr 13 '06 #1
6 1020
Or maybe I'm mixing up what we call a "classmethod" with what we could
call an "instance method" ?

Apr 13 '06 #2
"Thomas Girod" <gi****@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
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.)
Apr 13 '06 #3
On 2006-04-13, Thomas Girod <gi****@gmail.com> wrote:
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.
Apr 13 '06 #4
It's alright I found where my mistake came from. I was misunderstanding
the meaning of "classmethod", thinking of it as an instance method.

Apr 13 '06 #5
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 'o****@xiludom.gro'.split('@')])"
Apr 13 '06 #6
Thomas Girod wrote:
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 !-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Madestro | last post by:
Hi guys, I am making a small program to retrieve e-mails from POP accounts. I got all the e-mail parsing stuff figured out, but I cannot seem to come up with a way to find out which e-mails are...
3
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am...
20
by: Razvan | last post by:
Hi ! Is there any difference between new X and new X() ?! Regards, Razvan
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
5
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new(...
1
by: scorpiotgs | last post by:
I downloaded a script for a menu (below) but I want all the links i this part(ze_menu3) to open in a new window. this probably a noob question, but i couldnt figure it out, so pleas help me ...
15
by: Steve | last post by:
I have a form with about 25 fields. In the BeforeUpdate event of the form, I have code that sets the default value of each field to its current value. For a new record, I can put the focus in any...
7
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls...
3
by: Grizlyk | last post by:
Hi, people. Can anybody explain me "multiple 'new' at single line" behavior. Consider: p::p(void*); p::p(void*,void*); new A( p(new B), p( new C(p(new D), p(new E)) ), p(new F));
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.