Is this an OK design?
David Goodger
goodger at python.org
Thu Apr 15 08:42:42 EDT 2004
urnerk at qwest.net wrote:
> ---- pv1.py ----
>
> class P(object):
> def __init__(self, amap)
> self.amap = amap
> self.P = self.__class__ # <-- store type of instance
>
> --- pv2.py ---
>
> import pv1
> class P(pv1.P):
>
> def __invert__(self):
> # newmap = inverted self.amap
> return self.P(newmap) # <--- class is now object attribute
...
> What I'd like to know is: is my solution perverse or Pythonic?
> Is there a better way?
Whenever I've had to do the same thing, I'd just say:
return self.__class__(newmap)
Or make a specialized method, like:
def new(self, *args):
return self.__class__(*args)
In other words, say it directly without the indirection of a class
attribute.
Unless, of course, you want to modify the attribute for some
reason. Even then, I'd change the name to be more
self-documenting.
-- David Goodger
More information about the Python-list
mailing list