Issue with new-style classes and operators
sismex01 at hebmex.com
sismex01 at hebmex.com
Mon Nov 25 17:40:00 EST 2002
> From: Jan Decaluwe [mailto:jan at jandecaluwe.com]
> Sent: Monday, November 25, 2002 4:39 PM
>
> Alex Martelli wrote:
> >
> > So, to wrap an arbitrary object X in such a way that X's special
> > methods will be used by any operator, you need to ensure you do
> > the wrapping with a _class_ that exposes said special methods.
> > That's generally easiest to do with a factory function with a
> > nested class that uses inheritance, but you have many other choices
> > depending on what exactly you're trying to accomplish.
>
> Mm, I think I see, thanks for that. (I'll try again to
> understand this from the documentation.) However, if I use
> inheritance from an immutable type, the subtype is automatically
> immutable also, right?
Maybe not...
Mind you, I don't know why someone would want to do this.
>>> class MuTint(int):
def __init__(self):
self._items = {}
def __getitem__(self, item):
return self._items[item]
def __setitem__(self, item, value):
self._items[item] = value
def __delitem__(self, item):
del self._items[item]
>>> m = MuTint()
>>> m
0
>>> m["hello"] = 2
>>> m["goodbye"] = 3
>>> m
0
>>> print m
0
>>> m["hello"]
2
> So I wanted mutability, I'm back to delegation ... and apparently
> stuck with new-style classes ???
>
> Jan
But what's to great about old-style classes?
-gustavo
More information about the Python-list
mailing list