Really stupid question regarding PEP 252 and type/class unification

Jacob Kaplan-Moss jacobkm at dont.spam.me.cats.ucsc.edu
Thu Aug 23 18:59:04 CEST 2001


[snip]
> > So here are some questions. Suppose I derive a class from
> > a base type:
> > 
> >     class MyInt(type(0)):
> >         pass
> > 
> >     myInt = MyInt()
> >     myInt = 5                     # Will this work? 
> 
> Of course these bindings are independent. This works too:
>     myInt = 5
>     myInt = "spam"

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?  That is:

>>> 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

but now:

>>> isinstance(x, MyInt)
0

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)"?

Jacob



More information about the Python-list mailing list