polymorphism w/out signatures?
Peter Otten
__peter__ at web.de
Fri May 7 04:39:57 EDT 2004
Larry Bates wrote:
> I use type() quite a lot. I poked around and couldn't
> find where it was being deprecated. I did see that
> types module is slated for deprecation. Maybe I
> overlooked it?
>
> I use:
>
> class foo:
> _stringtype=type('')
> _tupletype=type(())
> _listtype=type([])
>
> def __init__(self, variable):
> if type(variable) == _stringtype:
> self.variable=variable # check for string type
> if type(variable) in (_listtype, tupletype):
> self.variable=str(variable) # check for list/tuple
I would change that to
def __init__(self, v):
self.variable = str(v)
The fact that str() is a function hides what is really going on, so I'll
make it explicit:
def __init__(self, v):
self.variable = v.__str__()
In general everything that differs from class to class should be delegated
to the class that needs the special behaviour. Of course for classes that
aren't under your control it may sometimes be more practical to
special-case them with isinstance() checks than to subclass.
Peter
More information about the Python-list
mailing list