newbe question
Don O'Donnell
donod at home.com
Wed Oct 10 20:16:05 EDT 2001
John Thingstad wrote:
>
> I wrote the following function to use polymorphism.
>
> def rootClassName(var):
> """Return the name of the root of a sigle inheretance class tree.
> If multiple inheritance is used the leftmost class is traversed."""
> if type(var) is ClassType:
> cls = var
> while len(cls.__bases__) != 0:
> cls = cls.__bases__[0]
> return cls.__name__
> else:
> return ""
>
> Is there a built in function to do this ?
>
Not that I know of.
But just a note on Pythonic style --
Okay (but a lot of unnecessary processing cycles going on here):
while len(cls.__bases__) != 0:
Better (!=0 is redundant at the end of a logical expression):
while len(cls.__bases__):
Best (an empty sequence evaluates as false in a boolean context):
while cls.__bases__:
Cheers,
Don
More information about the Python-list
mailing list