Help, how to override <= operator
Frank Niessink
frankn=news at cs.vu.nl
Thu Jun 3 09:34:53 EDT 1999
Greg Ewing <greg.ewing at compaq.com> wrote:
> Frank Niessink wrote:
>>
>> Clemens Hintze <cle at gmx.net> wrote:
>>
>> > class myint:
>> > i = 0
>>
>> This is a bit confusing because you don't need (and don't even use,
>> and probably don't _want_ to use) the class variable i.
> I beg to differ - I think that "declaring" instance variables
> in this way serves a useful documentation purpose, and I do
> so frequently.
But what is `declared' in the snippet above is a _class variable_!
If you are developing an integer class you probably don't want a
class variable...
This is the original posters' code:
> class myint:
> i = 0
> def __init__(self, n):
> self.i = n
> def __cmp__(self, b):
> if self.i < b.i: return -1
> if self.i > b.i: return 1
> return 0
The class variable i and the instance variable i are different. Example:
>>> a = myint(1)
>>> a.i
1
Because the instance variable i hides the class variable i we to access
the class variable indirectly:
>>> a.__class__.i
0
So my comment still stands: you don't need, don't use, and probably don't
want to use the class variable i (in this particular example of course).
Ciao, Frank
--
An RW6 was the sort of thing you only saw in the sort of magazines that were
designed to provoke civil unrest.
-- Douglas Adams, 'Mostly Harmless'
More information about the Python-list
mailing list