What is the class of myobj?

Mark McEahern marklists at mceahern.com
Tue Oct 15 20:47:35 EDT 2002


[JXStern]
> I want to write something like:
> 
> If ClassOf(myobj) == "MyClass": print "OK!"
>   or
> If ClassOf(myobj) == MyClass: print "OK!"   # no quotes on class
> 
> The best I've found in Python is based on str(myobj.__class__).
> 
> What's the official way?

$ python
Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class foo:pass
...
>>> class bar(foo):pass
...
>>> f = bar()
>>> isinstance(f, foo)
1
>>> isinstance(f, bar)
1
>>> isinstance(bar, foo)
0
>>> issubclass(bar, foo)
1
>>> print f.__class__.__name__
bar
>>> f.__class__.__name__ == "bar"
1

-





More information about the Python-list mailing list