Python技巧之运行时访问类名和函数名
文章目录
你可以在运行时获取字符对象名,技巧是使用下划线包裹的属性:
# You can get the name of
# an object's class as a
# string:
>>> class MyClass: pass
>>> obj = MyClass()
>>> obj.__class__.__name__
'MyClass'
# Functions have a
# similar feature:
>>> def myfunc(): pass
>>> myfunc.__name__
'myfunc'
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论