Hi,
How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?
Was thinking maybe the inspects module might have helped me out here, but it doesn't seem to give me what I want and short of parsing the __class__ member, I'm not sure how to get at this information.
Thanks Dan
From stackoverflow
-
type() ?
>>> class A(object): ... def whoami(self): ... print type(self).__name__ ... >>> >>> class B(A): ... pass ... >>> >>> >>> o = B() >>> o.whoami() 'B' >>>Dan : this is the same as the __class__ member, but i have to parse this result by hand, which is a bit annoying... -
Have you tried the
__name__attribute of the class? iex.__class__.__name__will give you the name of the class, which I think is what you want.>>> import itertools >>> x = itertools.count(0) >>> x.__class__.__name__ 'count'It should work similarly from wherever you call it.
-
Do you want the name of the class as a string?
instance.__class__.__name__
0 comments:
Post a Comment