What method do I call to get the name of a class?
+10
A:
It's not a method, it's a field. The field is called __name__
. class.__name__
will give the name of the class as a string. object.__class__.__name__
will give the name of the class of an object.
clahey
2008-09-16 18:27:58
Markdown considers __name__ to meen name in bold, you need to escape it in some way
Mr Shark
2008-09-16 18:31:03
Thanks for the comment. Fixed.
clahey
2008-09-16 19:42:26
+14
A:
In [1]: class test(object):
...: pass
...:
In [2]: test.__name__
Out[2]: 'test'
Mr Shark
2008-09-16 18:28:51
A:
In [8]: str('2'.class)
Out[8]: ""
In [9]: str(len.class)
Out[9]: ""
In [10]: str(4.6.class)
Out[10]: ""
Or, as was pointed out before,
In [11]: 4.6.class.name
Out[11]: 'float'
I think your underscores got eaten by markdown. You meant: 4.6.4.6.__class__.__name__
Joe Hildebrand
2008-09-16 23:43:29
A:
I agree with Mr.Shark, but if you have an instance of a class, you'll need to use it's __class__ member:
>>> class test():
... pass
...
>>> a_test = test()
>>>
>>> a.__name__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: test instance has no attribute '__name__'
>>>
>>> a.__class__
<class __main__.test at 0x009EEDE0>
Jon Cage
2008-09-17 13:26:31