Home  >  Article  >  Backend Development  >  Python方法间调用,self.方法跟cls=类名;cls.方法,二种方式有什么区别?

Python方法间调用,self.方法跟cls=类名;cls.方法,二种方式有什么区别?

WBOY
WBOYOriginal
2016-06-06 16:23:402084browse

我看到tornado有个聊天室的demo,类MessageMixin的方法间调用使用了cls=类;cls.方法的方式,自己改成self.方法似乎跟之前的调用方式没有区别,不知道这两种方式是不是有什么深层的不同?

回复内容:

我写过的一篇文章

什么是method?

function就是可以通过名字可以调用的一段代码,我们可以传参数进去,得到返回值。所有的参数都是明确的传递过去的。
method是function与对象的结合。我们调用一个方法的时候,有些参数是隐含的传递过去的。下文会详细介绍。

instancemethod
<code class="language-text">In [5]: class Human(object):
   ...:     def __init__(self, weight):
   ...:         self.weight = weight
   ...:     def get_weight(self):
   ...:         return self.weight
   ...:     

In [6]: Human.get_weight
Out[6]: <unbound method human.get_weight>
</unbound></code>
假设有类c,且o是类c的对象,那么
虽然执行过程上有点细微差别,但从结果上说,
o.method(arg, ...) 等价于 c.method(o, arg, ...)

有了不懂的问题,先Google和先问,二种方式有什么区别?

顺便帮你把大小写改正了,不谢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn