Is it possible to get the name of a subclass? For example:
class Foo:
def bar(self):
print type(self)
class SubFoo(Foo):
pass
SubFoo().bar()
will print: < type 'instance' >
I’m looking for a way to get "SubFoo".
I know you can do isinstance, but I don’t know the name of the class a priori, so that doesn’t work for me.
Subclassing from
objectgives you new-style classes (which are not so new any more – python 2.2!) Anytime you want to work with the self attribute a lot you will get a lot more for your buck if you subclass from object. Python’s docs … new style classes. Historically Python left the old-style wayFoo()for backward compatibility. But, this was a long time ago. There is not much reason anymore not to subclass from object.