函数与方法的区别 / Distinction of Function and Method


关于函数与方法的区别,可根据两者的定义看出,

函数function -- A series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body.

方法method -- A function which is defined inside a class body. If called as an attribute of an instance of that class, the method will get the instance object as its first argument (which is usually called self).

从定义上来看,可以说,方法是一种依赖于类对象特殊函数

 class Foo():
def foo(self):
pass
print(type(Foo().foo))
print(type(Foo.foo))
# print(Foo().foo.__class__)
# print(Foo.foo.__class__)

从输出中可以看出,脱离了类实例的方法依旧是函数(此处的 type 等价于调用了内置的 __class__ 函数)。

<class 'method'>
<class 'function'>

参考链接


http://blog.csdn.net/peachpi/article/details/6047826

Python的程序结构[4] -> 函数/Function[0] -> 函数与方法的区别的更多相关文章

  1. Python的程序结构[5] -> 模块/Module[0] -> 内建模块 builtins

    builtins 内建模块 / builtins Module 在Python的模块中,有一种特殊模块,无需导入便可以使用,其中包含了许多内建函数与类. builtins 模块内容 / builtin ...

  2. Python的程序结构[2] -> 类/Class[0] -> 类的特殊属性

    类的特殊属性 / Special Property of Class Python 中通过 class 进行类的定义,类可以实例化成实例并利用实例对方法进行调用. 类中还包含的一些共有的特殊属性. 特 ...

  3. Python的程序结构[3] -> 变量/Variable[0] -> 变量类型

    变量类型 / Variable Type 在 Python 中,变量主要有以下几种,即全局变量,局部变量和内建变量, 全局变量 / Global Variable 通常定义于模块内部,大写变量名形式存 ...

  4. 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别

    [面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...

  5. Python的程序结构[4] -> 函数/Function[2] -> 匿名函数

    匿名函数 / Anonymous Function 匿名函数是一种不需要绑定函数名的函数 (i.e. functions that are not bound to a name).匿名函数通过 la ...

  6. Python的程序结构[7] -> 生成器/Generator -> 生成器浅析

    生成器 / Generator 目录 关于生成器 生成器与迭代器 生成器的建立 通过迭代生成器获取值 生成器的 close 方法 生成器的 send 方法 生成器的 throw 方法 空生成器的检测方 ...

  7. Python的程序结构[1] -> 方法/Method[2] -> 魔术方法 __init__ / __del__ / __new__

    魔术方法 / Magic Method 魔法方法就是可以给你的类增加魔力的特殊方法(实质应称为特殊方法,魔术方法在JavaScript中有所体现,对象具有不透明特性,而且无法在自定义对象中模拟这些行为 ...

  8. Python的程序结构[1] -> 方法/Method[4] -> 魔术方法 __call__ / __str__ / __repr__

    __call__ 方法 __call__ 是当对象被调用时会调用的方法,允许一个对象(类的实例等)像函数一样被调用,也可以传入参数. 1 class Foo(): 2 def __init__(sel ...

  9. Python的程序结构[4] -> 函数/Function[1] -> 内建函数

    内建函数 / Built-in Function or Method Python中有许多的内建函数(查看内建模块部分),此处将对内建函数进行介绍 内建函数 ord / built-in functi ...

随机推荐

  1. 《Cracking the Coding Interview》——第16章:线程与锁——题目1

    2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...

  2. 《Cracking the Coding Interview》——第12章:测试——题目1

    2014-04-24 23:10 题目:找出下面代码里的错误. 解法:请看下面. 代码: // 12.1 What's wrong with the following code segment? # ...

  3. nginx配置及HTTPS配置示例

    一.nginx简单配置示例 user www www; worker_processes ; #error_log logs/error.log; #error_log logs/error.log ...

  4. katalon系列二:selenium IDE的替代者——Katalon Recorder

    Katalon Recorder是和selenium IDE一样的一个浏览器插件,可以录制web上的操作并回放,但我个人感觉Katalon Recorder更好用.大家可以直接在chrome商店下载安 ...

  5. python3知识点之---------字符串的介绍

    1. 定义 其实字符串就是一系列字符,用引号括起来的就是字符串,其中的引号可以是单引号或者双引号. 比如 "This is a string"   'This is a strin ...

  6. 2 27re.py

    """ 匹配目标 """ # import re # content = 'Hello 123 4567 World_This is a R ...

  7. StringBuilder_学习笔记

    参考:https://www.jianshu.com/p/160c9be0b132 连接符号 "+" 本质 字符串变量(非final修饰)通过 "+" 进行拼接 ...

  8. Oracle设置用户密码永不过期

    1.查看用户的profile是那个,一般是default: select username, profile from dba_users; 2.查看指定概要文件(如default)的密码有效期设置: ...

  9. Redis客户端命令

    Redis客户端命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. ...

  10. 【bzoj3866】The Romantic Hero dp

    题目描述 给你n个数,从中选出两个不相交非空集合S和T,使得S中的每一个元素都在T集合的前面,并且S集合中的所有数的亦或等于T集合中的所有数的与,求方案数 mod 10^9+7. 输入 The fir ...