Really stupid question regarding PEP 252 and type/class unification

Neil Schemenauer nas at python.ca
Thu Aug 23 20:01:38 CEST 2001


Jacob Kaplan-Moss wrote:
> I think I see what Russel is getting at here... the point is, myInt is 
> an instance of class MyInt when it is first created, but what if I want 
> to change the value of myInt without modifying the class?

You can't.  ints are immutable.

> >>> class MyInt(int):  
> ...   pass
> >>> x = MyInt()
> >>> isinstance(x, MyInt)
> 1
> 
> So here x is still a MyInt object.  Now I want to change the x so that x 
> holds the value 5 but is still a myInt object.  So I try:
> 
> >>> x = 5

You are quite confused about how Python works.  The above statement
binds the name "x" to the object "5".

> So, the question is, can I treat a MyInt the same way as I can treat an 
> int?  Or do I have to do something like "x.set(5)" or "x = MyInt(5)"?

Eventually you should be able to treat it just like an int.

  Neil




More information about the Python-list mailing list