为什么我的班级方法没有约束?
问题描述:
这是我正在研究的一个python类的片段。 (对不起,我不能去太多的细节。我的组织变得有点坐立不安这样的事情。为什么我的班级方法没有约束?
class my_class:
@classmethod
def make_time_string(cls, **kwargs):
# does some stuff
@classmethod
def make_title(cls, **kwargs):
# does some other stuff
@classmethod
def give_local_time(cls, **kwargs):
# yet more stuff
@classmethod
def data_count(cls, **kwargs):
# you guessed it
到目前为止,一切都很好,我应该想到,但我使用这个类有麻烦。而且它原来,原因是后两种方式都绑定:
>>> my_class.make_time_string
<bound method classobj.make_time_string of <class __main__.my_class at 0x1cb5d738>>
>>> my_class.make_title
<bound method classobj.make_title of <class __main__.my_class at 0x1cb5d738>>
>>> my_class.give_local_time
<unbound method my_class.give_local_time>
>>> my_class.data_count
<unbound method my_class.data_count>
对不起,再一次,我不能更多关于即将出台的内容,但任何人都可以,为什么这些最后两种方法想出一个理由。应该在前两个绑定(正确)时解除绑定?
答
我试过你的公司德,它适用于我。
所以,你需要提供一个更好的例子。
我最好的猜测是你在某种程度上错字@classmethod
或不再在类定义或什么。也许你会覆盖classmethod
修饰器?
这里是我的代码:
class my_class:
@classmethod
def make_time_string(cls, **kwargs):
pass
@classmethod
def make_title(cls, **kwargs):
pass
@classmethod
def give_local_time(cls, **kwargs):
pass
@classmethod
def data_count(cls, **kwargs):
pass
print my_class.make_time_string
print my_class.make_title
print my_class.give_local_time
print my_class.data_count
后可重复的最小例子。我猜你是在某些情况下从其他类型的实例中查询类型。 –
这里工作也很好。 'dir(my_class)'说什么? – flup
好吧,我是新来的python类,所以请原谅这个错误。在我的方法没有绑定的地方,我有* instance *方法同名。我认为python能够分辨my_class.method()和my_instance.method()之间的区别。猜猜我错了。我没有真正看到这个写在任何地方。有人能指点我吗?谢谢。 –