polymorphism w/out signatures?
Larry Bates
lbates at swamisoft.com
Fri May 7 11:03:42 EDT 2004
Duncan,
Thanks for your reply to my post. As all programmers
I have adopted coding "techniques" that I see in others
code. The technique I suggested is lifted directly
from ReportLab code. I don't know if it predates the
isinstance function or not (which may explain why it
is not used). BTW-Your example that includes basestring
apparently only works on Python 2.3. Anyway, I
learned something today.
Thanks again,
Larry Bates
Syscon, Inc.
"Duncan Booth" <me at privacy.net> wrote in message
news:Xns94E25EB583B9Bduncanrcpcouk at 127.0.0.1...
> "Larry Bates" <lbates at swamisoft.com> wrote in
> news:lMWdncBA4LRcAgfd4p2dnA at comcast.com:
>
> > I use:
> >
> > class foo:
> > _stringtype=type('')
> > _tupletype=type(())
> > _listtype=type([])
>
> You could just use the builtin names already supplied for these: str,
> tuple, and list have the same values you just assigned to _stringtype,
> _tupletype, and _listtype. Also, you forgot about unicode.
>
> >
> > 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
> > .
>
> If anyone has been subclassing str, list or tuple this won't work. You may
> not subclass builtin types very often, but it doesn't really hurt to use
> isinstance instead. That's why it is better to write:
>
> if isinstance(variable, (str, unicode)):
> ...
>
> or:
>
> if isinstance(variable, basestring):
> ...
>
> as either of these will catch subclassing.
>
More information about the Python-list
mailing list