什么是method?

function就是可以通过名字可以调用的一段代码,我们可以传参数进去,得到返回值。所有的参数都是明确的传递过去的。

method是function与对象的结合。我们调用一个方法的时候,有些参数是隐含的传递过去的。下文会详细介绍。

instancemethod

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]:

这告诉我们get_weight是一个没有被绑定方法,什么叫做未绑定呢?继续看下去。

In [7]: Human.get_weight()

---------------------------------------------------------------------------

TypeError Traceback (most recent call last)

/home/yao/learn/insight_python/ in ()

----> 1 Human.get_weight()

TypeError: unbound method get_weight() must be called with Human instance as first argument (got nothing instead)

未绑定的方法必须使用一个Human实例作为第一个参数来调用啊。那我们来试试

In [10]: Human.get_weight(Human(45))

Out[10]: 45

果然成功了,但是一般情况下我们习惯这么使用。

In [11]: person = Human(45)

In [12]: person.get_weight()

Out[12]: 45

这两种方式的结果一模一样。我们看下官方文档是怎么解释这种现象的。

When an instance attribute is referenced that isn't a data attribute, its class is searched.

If the name denotes a valid class attribute that is a function object, a method object is

created by packing (pointers to) the instance object and the function object just found together

in an abstract object: this is the method object. When the method object is called with an

argument list, a new argument list is constructed from the instance object and the argument list,

and the function object is called with this new argument list.

原来我们常用的调用方法(person.get_weight())是把调用的实例隐藏的作为一个参数self传递过去了, self 只是一个普通的参数名称,不是关键字。

In [13]: person.get_weight

Out[13]: >

In [14]: person

Out[14]: <__main__.human at>

我们看到get_weight被绑定在了 person 这个实例对象上。

总结下

instance method 就是实例对象与函数的结合。

使用类调用,第一个参数明确的传递过去一个实例。

使用实例调用,调用的实例被作为第一个参数被隐含的传递过去。

classmethod

In [1]: class Human(object):

...: weight = 12

...: @classmethod

...: def get_weight(cls):

...: return cls.weight

In [2]: Human.get_weight

Out[2]: >

我们看到get_weight是一个绑定在 Human 这个类上的method。调用下看看

In [3]: Human.get_weight()

Out[3]: 12

In [4]: Human().get_weight()

Out[4]: 12

类和类的实例都能调用 get_weight 而且调用结果完全一样。

我们看到 weight 是属于 Human 类的属性,当然也是 Human 的实例的属性。那传递过去的参数 cls 是类还是实例呢?

In [1]: class Human(object):

...: weight = 12

...: @classmethod

...: def get_weight(cls):

...: print cls

In [2]: Human.get_weight()

In [3]: Human().get_weight()

我们看到传递过去的都是 Human 类,不是 Human 的实例,两种方式调用的结果没有任何区别。cls 只是一个普通的函数参数,调用时被隐含的传递过去。

总结起来

classmethod 是类对象与函数的结合。

可以使用类和类的实例调用,但是都是将类作为隐含参数传递过去。

使用类来调用 classmethod 可以避免将类实例化的开销。

staticmethod

In [1]: class Human(object):

...: @staticmethod

...: def add(a, b):

...: return a + b

...: def get_weight(self):

...: return self.add(1, 2)

In [2]: Human.add

Out[2]:

In [3]: Human().add

Out[3]:

In [4]: Human.add(1, 2)

Out[4]: 3

In [5]: Human().add(1, 2)

Out[5]: 3

我们看到 add 在无论是类还是实例上都只是一个普通的函数,并没有绑定在任何一个特定的类或者实例上。可以使用类或者类的实例调用,并且没有任何隐含参数的传入。

In [6]: Human().add is Human().add

Out[6]: True

In [7]: Human().get_weight is Human().get_weight

Out[7]: False

add 在两个实例上也是同一个对象。instancemethod 就不一样了,每次都会创建一个新的 get_weight 对象。

总结下

当一个函数逻辑上属于一个类又不依赖与类的属性的时候,可以使用 staticmethod。

使用 staticmethod 可以避免每次使用的时都会创建一个对象的开销。

staticmethod 可以使用类和类的实例调用。但是不依赖于类和类的实例的状态。

python method_用实例分析Python中method的参数传递过程相关推荐

  1. python中定义一个类、实例化时传入的参数如何传递_用实例分析Python中method的参数传递过程...

    什么是method? function就是可以通过名字可以调用的一段代码,我们可以传参数进去,得到返回值.所有的参数都是明确的传递过去的. method是function与对象的结合.我们调用一个方法 ...

  2. Python 爬虫实战:分析豆瓣中最新电影的影评

    Python 爬虫实战:分析豆瓣中最新电影的影评 接触python时间不久,做些小项目来练练手.前几天看了<战狼2>,发现它在最新上映的电影里面是排行第一的,如下图所示.准备把豆瓣上对它的 ...

  3. python经典程序实例代码,python编程应用实例

    python递归算法经典实例有哪些? 程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法. ...

  4. python多进程编程实例_[python] Python多进程编程技术实例分析

    这篇文章主要介绍了Python多进程编程技术,包括了线程.队列.同步等概念及相关的技巧总结,需要的朋友可以参考下 本文以实例形式分析了Python多进程编程技术,有助于进一步Python程序设计技巧. ...

  5. 一、Python时间序列小波分析——实例分析

    小波分析是在Fourier分析基础上发展起来的一种新的时频局部化分析方法.小波分析的基本思想是用一簇小波函数系来表示或逼近某一信号或函数. 小波分析原理涉及到傅里叶变换,并有多种小波变换,有点点小复杂 ...

  6. python蟒蛇绘制实例分析_2.4蟒蛇绘制程序分析

    蟒蛇绘制程序分析 python蟒蛇实例 import turtle def drawSnake(rad, angle, len, neckrad): for i in range(len): turt ...

  7. python蟒蛇绘制实例分析_011 实例2-Python蟒蛇绘制

    一."Python蟒蛇绘制"问题分析 1.1 Python蟒蛇绘制 用程序绘制一条蟒蛇 貌似很有趣,可以来试试 先学会蟒蛇绘制,再绘朵玫瑰花送给TA 设计蟒蛇的基本形状: 问题1: ...

  8. python如何做敏感度分析_Python中的模型敏感度分析(使用Salib)

    敏感度分析的基础概念 文本主要参考了维基百科(对其中的关键部分进行了摘选了翻译):https://en.wikipedia.org/wiki/Sensitivity_analysis​en.wikip ...

  9. python 聚类算法包_Python聚类算法之DBSACN实例分析 python怎么用sklearn包进行聚类

    python 怎么可视化聚类的结果 science 发表的聚类算法的python代码 测试数据长什...说明你的样本数据中有nan值,通常是因为原始数据中包含空字符串或None值引起的. 解决办法是把 ...

最新文章

  1. 2016 ACM / ICPC Asia dalian Regional Contest 题解(11 / 11)【每日亿题2021 / 2 / 17】
  2. 阿里云ECS主机自定义进程监控
  3. vue 中provide的用法_聊聊Vue中provide/inject的应用详解
  4. lcd和服务器同时显示温度,51单片机LCD1602如何同时显示温度和时间
  5. 【贪心】最大连续数列的和
  6. ffmpeg 静态库使用,undefined reference错误
  7. c语言程序申请管理员权限,vc 软件 要管理员运行 vc 管理员权限运行
  8. 使用php函数判断数字,PHP 几个常用数字判断函数的简单示例
  9. Strusts2笔记6--拦截器
  10. php 多附件上传插件下载,大文件、多文件分片上传插件
  11. 《Head First 设计模式》例子的C++实现(2 观察者模式)
  12. Python爬取某境外网站漫画,心血来潮,爬之
  13. 计算机三级考点3:构建宽带城域网的基本技术与方案。
  14. 基础篇:6.3)形位公差-要素 Feature
  15. BUUCTF刷题记录
  16. 阿里云移动测试平台使用教程
  17. Chrome浏览器:分享几个好用的谷歌浏览器拓展程序
  18. iPad/iPhone内存管理四之viewDidUnload/dealloc详细解说
  19. dm服务器未能启动,DM 达梦数据库 创建服务 无法创建目录_REPLACE_SELF_DM_HOME 错误解决方法...
  20. 美国研究生院计算机数据科学排名,美国研究生数据科学专业排名

热门文章

  1. webRTC之Mesh/MCU/SFU通信架构区别(九)
  2. Ubuntu18.04之搜狗输入法
  3. Input调用流程(好文)
  4. ubuntu之间传输文件
  5. map.setTerrain is not a function
  6. Eclipse中查看JDK类库的源代码
  7. android4.0教程,图文教程现身,在Win7等系统里跑起Android4.0
  8. Liist Object ? 获取方式
  9. 浙江独立学院计算机专业排名2015,2014-2015年中国独立学院排名
  10. 支持向量机smo matlab,理解支持向量机(三)SMO算法