可以在班级名称上找到classmethod
我正在尝试通过className .AttributeError问题调用classMethod。
当我使用@singleton时,我无法使用className.functionname .functionname .in className()。functionName。 为什么会发生这种情况?
def singleton(cls):
'''
单例
:param cls:
:return:
'''
_instance = {}
def _singleton(*args, **kargs):
if cls not in _instance:
_instance[cls] = cls(*args, **kargs)
# print(type(_instance[cls])) <class '__main__.Coco'>
return _instance[cls]
return _singleton
@singleton
class Coco():
# def __new__(cls, *args, **kwargs):
# if not hasattr(Coco, "_instance"):
# if not hasattr(Coco, "_instance"):
# Coco._instance = object.__new__(cls)
# print(type(Coco._instance))
# return Coco._instance
def __init__(self):
print('coco')
@classmethod
def get_info(cls):
print('coco is 18 ages old')
# print(Coco().get_info())
print(Coco.get_info())
例外
Traceback (most recent call last):
File "/Users/coco/Automation/AutoTestRes/scripts/python/coco.py", line 36, in <module>
print(Coco.get_info())
AttributeError: 'function' object has no attribute 'get_info'
I am trying to invoke classmethod over classname .AttributeError problem occurs
When I use @singleton ,I can't run with classname.functionname .It's must be classname().functionname
Why does this happen?
def singleton(cls):
'''
单例
:param cls:
:return:
'''
_instance = {}
def _singleton(*args, **kargs):
if cls not in _instance:
_instance[cls] = cls(*args, **kargs)
# print(type(_instance[cls])) <class '__main__.Coco'>
return _instance[cls]
return _singleton
@singleton
class Coco():
# def __new__(cls, *args, **kwargs):
# if not hasattr(Coco, "_instance"):
# if not hasattr(Coco, "_instance"):
# Coco._instance = object.__new__(cls)
# print(type(Coco._instance))
# return Coco._instance
def __init__(self):
print('coco')
@classmethod
def get_info(cls):
print('coco is 18 ages old')
# print(Coco().get_info())
print(Coco.get_info())
Exception
Traceback (most recent call last):
File "/Users/coco/Automation/AutoTestRes/scripts/python/coco.py", line 36, in <module>
print(Coco.get_info())
AttributeError: 'function' object has no attribute 'get_info'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在Python中使用装饰器时,如下:
...,这相当于这样做:
这意味着变量
class_name
不再是一类,而是什么,而是任何内容decorator_name
的返回值是。在您的情况下,类Decorator
Singleton
返回函数_Singleton
,而不是实际类。因此,当您说:相同
...,这与说: ...在功能中
。因此,您将获得
attributeError
,因为该函数现在具有名称coco
,它没有该属性。要访问类的属性,您需要运行该功能,因为这将返回类的实例,该实例将具有属性。
不再有可能从全局范围访问课程本身。
When you use a decorator in Python, like this:
..., this is equivalent to doing this:
This means that the value of the variable
class_name
is no longer necessarily a class, but instead it is whatever the return value ofdecorator_name
is.In your case, the class decorator
singleton
returns the function_singleton
, not the actual class. So when you say:..., this is the same as saying:
...within the function.
Therefore, you get an
AttributeError
, because the function, which now has the nameCoco
, does not have that attribute.To access the attribute of the class, you need to run the function because this will return an instance of the class, which will have the attribute.
It is no longer possible to access the class itself from the global scope.