[Python-bugs-list] [ python-Bugs-483236 ] subclass of int

noreply@sourceforge.net noreply@sourceforge.net
Fri, 23 Nov 2001 18:47:39 -0800


Bugs item #483236, was opened at 2001-11-18 18:50
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=483236&group_id=5470

>Category: Type/class unification
Group: Python 2.2
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
>Assigned to: Guido van Rossum (gvanrossum)
Summary: subclass of int

Initial Comment:
a in the following example should be 220

>>> class ticks(int):
...   __slots__ = ['clk']
...   def __init__(self,dd):
...     dd*=self.clk
...     int.__init__(self,dd)
...
>>> ticks.clk=11
>>> a=ticks(20)
>>> a
20

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-11-23 18:47

Message:
Logged In: YES 
user_id=6380

Sorry, you can't reinitialize an int object's value by
calling int.__init__() again. The int value initialization
happens in int.__new__() and that returns a new object.

You could try this:

>>> class ticks(int):
...     def __new__(cls, dd=0):
...         dd*=20
...         return int.__new__(cls, dd)

BTW, your use of __slots__ = ['clk'] followed by an
assignment to ticks.clk doesn't make sense; the __slots__
magic creates *instance* variables. But that's not related
to your complaint.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=483236&group_id=5470