Python3.x未定义名称,使用@staticmethod,@classmethod,使用';打字';特征
我一直在用Python3.6编写一些代码,其中类状态可以保存到磁盘,然后加载。它大致是这样的:Python3.x未定义名称,使用@staticmethod,@classmethod,使用';打字';特征,python,python-3.x,static-methods,typing,Python,Python 3.x,Static Methods,Typing,我一直在用Python3.6编写一些代码,其中类状态可以保存到磁盘,然后加载。它大致是这样的: class MyClass(object): @staticmethod def Load(filename) -> MyClass: return MyClass() print(MyClass.Load('test.pkl')) 使用键入时,我收到一个错误: Traceback (most recent call last): File "tmp/te
class MyClass(object):
@staticmethod
def Load(filename) -> MyClass:
return MyClass()
print(MyClass.Load('test.pkl'))
使用键入时,我收到一个错误:
Traceback (most recent call last):
File "tmp/test.py", line 2, in <module>
class MyClass(object):
File "tmp/test.py", line 4, in MyClass
def Load(MyClass, filename) -> MyClass:
NameError: name 'MyClass' is not defined
使用@classmethod装饰器时也会发生相同的错误。关于打字功能的工作方式,我有没有遗漏什么?有没有更“pythonic”的方式来编写此代码?(加载实现与默认构造函数明显不同,因此希望编写不同的函数,而不是使用关键字参数)
>> python tmp/test.py
<__main__.MyClass object at 0x7f249449a550>
class MyClass(object):
@staticmethod
def Load(filename):
return MyClass()
print(MyClass.Load('test.pkl'))