At Monday 15/1/2007 19:28, Jim B. Wilson wrote:

Am I nuts? Or only profoundly confused? I expected the this little script
to print "0":

class foo(int):
  def __init__(self, value):
    self = value & 0xF

print foo(0x10)

Instead, it prints "16" (at least on python 2.4.4 (Linux) and 2.5 (Wine).

Integers are immutable. Insert a print statement and you'll see your __init__ is never called. Anyway, what would you expect from "self = something"?
Use __new__ instead:

py> class foo(int):
...   def __new__(cls, value):
...     return int(value & 0xF)
...
py> foo(1)
1
py> foo(0x10)
0

See "Special method names" inside the Python Reference Manual.


--
Gabriel Genellina
Softlab SRL

        

        
                
__________________________________________________ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to