Subclasses in Python

Terry Reedy tjreedy at udel.edu
Thu Apr 29 13:58:10 EDT 2004


You have needed edit already.  To explain a bit more ...

>     #Private methods for class Player
>     def __init__(self,name):
>         self.name = name
>         self.strength = self.__class__.initial_strength
>         self.__class__.n +=1
>         print self.__class__

print object # is same as
print str(object) # ie, prints stringifies everything

> Interestingly, if I do a class comparison of the form
>
> if self.__class__== Alien:
>     foo
> elif self.__class__== Player
>     bar
>
> The comparison proceeds correctly.

Here self.__class__ is left as the class object, as usual, and not
stringified.

> How can I get it to print the class
> name cleanly? Do I have to convert <class '__main__.Alien'> to a
> string

That is what you did to print it '-)

>and then use one or more string functions to clean it up?

Fortunately not.

Terry J. Reedy









More information about the Python-list mailing list