Python:如何引用一个类本身?(Python: How to refer a class inside itself?)
python
试图引用一个类本身。 例如
class Test: FOO_DICT = {1 : Test.bar} # NameError: name 'Test' is not defined @staticmethod def bar(): pass这种用法有意义吗? 有更好的选择吗? 谢谢!
Trying to refer a class inside itself. For e.g.
class Test: FOO_DICT = {1 : Test.bar} # NameError: name 'Test' is not defined @staticmethod def bar(): passDoes this usage make sense? Any better alternatives? Thanks!
最满意答案
如果您希望字典在类中:首先定义函数并删除Test:
class Test: @staticmethod def bar(): pass FOO_DICT = {1: bar}If you want the dictionary to be in the class: define the function first and remove Test:
class Test: @staticmethod def bar(): pass FOO_DICT = {1: bar}更多推荐
发布评论