我希望使python __magic__
能够访问,而无需创建一个类来激活它们
例如
class Group:
_groups = []
# a static var
@classmethod
def __len__(cls):
return len(cls._groups)
# return the len of the static var
但是当我运行它
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type 'type' has no len()
要使其正常工作,我需要输入Group.__len__()
,但这不是魔术方法,而只是一个长名称的函数
我为什么要
这使我的代码将来更易读,接受编写len(Group._groups)