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 ??? 6 1020
Or maybe I'm mixing up what we call a "classmethod" with what we could
call an "instance method" ?
"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.)
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.
It's alright I found where my mistake came from. I was misunderstanding
the meaning of "classmethod", thinking of it as an instance method.
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('@')])"
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('@')])" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
by: Razvan |
last post by:
Hi !
Is there any difference between new X and new X() ?!
Regards,
Razvan
|
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
=...
|
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...
| |
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(...
|
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
...
|
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...
|
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...
|
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));
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |