找到一个类的名称 [英] Finding the name of a class
问题描述
是否有函数或方法返回类或类的名称
实例?
类X(对象):
pass
X.amethod()或X()。amethod()应返回字符串X。
X().__ class__返回"< class''_ _ main __。X''>"我可以解析。呃。
或者我可以使用模块pyclbr。呃** 2。
Is there a function or method that returns the name of a class or class
instance?
class X(object):
pass
X.amethod() or X().amethod() should return the string "X".
X().__class__ returns "<class ''__main__.X''>" which I could parse. Ugh.
Or I could use module pyclbr. Ugh**2.
推荐答案
2004年5月12日星期三20:10:07 -0400,Edward C. Jones写道:
On Wed, 12 May 2004 20:10:07 -0400, Edward C. Jones wrote:
是否有函数或方法返回类或类的名称
实例?
类X(对象):
传递
X.amethod()或X()。amethod()应该返回字符串X。
X().__ class__返回"< class''__ main__ .X '' >"我可以解析。呃。
或者我可以使用模块pyclbr。呃** 2.
Is there a function or method that returns the name of a class or class
instance?
class X(object):
pass
X.amethod() or X().amethod() should return the string "X".
X().__class__ returns "<class ''__main__.X''>" which I could parse. Ugh.
Or I could use module pyclbr. Ugh**2.
我还没有找到更好的办法:
def classname(c):
sl =`c`.split('''')[0] [1:] .split(''。'')
return len(sl)> 1和sl [1]或sl [0]
I havent found any better way to do it:
def classname(c):
sl = `c`.split('' '')[0][1:].split(''.'')
return len(sl) > 1 and sl[1] or sl[0]
" Edward C. Jones"写道:
"Edward C. Jones" wrote:
是否有一个函数或方法返回一个类的名称或
类
实例?
类X(对象):
X.amethod()或X()。amethod()应返回字符串X。
X()。 __class__返回"< class''_ _ main __。X''>"我可以解析。呃。
或者我可以使用模块pyclbr。呃** 2.
Is there a function or method that returns the name of a class or
class
instance?
class X(object):
pass
X.amethod() or X().amethod() should return the string "X".
X().__class__ returns "<class ''__main__.X''>" which I could parse. Ugh.
Or I could use module pyclbr. Ugh**2.
使用__name__属性:
Use the __name__ attribute:
C类:传递
.... C .__ name__
''C''C().__ class __.__ name __
class C: pass .... C.__name__ ''C'' C().__class__.__name__
>
''C''
-
__ Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/
/ \美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& AIM erikmaxfrancis
\ __ /没有爱,仁慈就变成了自我主义。
- 小马丁·路德金博士
''C''
--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Without love, benevolence becomes egotism.
-- Dr. Martin Luther King, Jr.
>
Edward C. Jones写道:
Edward C. Jones writes:
X().__ class__返回"< class''__ main __。X''>"我可以解析。呃。
X().__class__ returns "<class ''__main__.X''>" which I could
parse. Ugh.
怎么样X().__ class __.__ name__
-
Paul
How about X().__class__.__name__
--
Paul
这篇关于找到一个类的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!