Python 区分方法和函数
def func():
print("我是函数") class Foo:
def chi(self):
print("我是吃") # print(func) # <function func at 0x0000000001D42E18>
f = Foo()
# f.chi() print(f.chi) # <bound method Foo.chi of <__main__.Foo object at 0x0000000002894A90>> # 野路子: 打印的结果中包含了function. 函数
# method . 方法 # 我们的类也是对象.
# 这个对象: 属性就是类变量
# 方法就是类方法
class Person:
def chi(self):
print("我要吃鱼") @classmethod
def he(cls):
print("我是类方法") @staticmethod
def pi():
print("泥溪镇地皮") p = Person()
Person.chi(1) # 不符合面向对象的思维 print(p.chi) # <bound method Person.chi of <__main__.Person object at 0x00000000028C4B70>>
print(Person.chi) # <function Person.chi at 0x00000000028989D8> # 实例方法:
# 1. 如果使用 对象.实例方法 方法
# 2. 如果使用 类.实例方法 函数 print(Person.he) # <bound method Person.he of <class '__main__.Person'>>
print(p.he) # <bound method Person.he of <class '__main__.Person'>> # 类方法都是 方法 print(Person.pi) # <function Person.pi at 0x0000000009E7F488>
print(p.pi) # <function Person.pi at 0x0000000009E7F488> # 静态方法都是函数 # 记下来
from types import FunctionType, MethodType # 方法和函数
from collections import Iterable, Iterator # 迭代器 class Person:
def chi(self): # 实例方法
print("我要吃鱼") @classmethod
def he(cls):
print("我是类方法") @staticmethod
def pi():
print("泥溪镇地皮") p = Person() print(isinstance(Person.chi, FunctionType)) # True
print(isinstance(p.chi, MethodType)) # True print(isinstance(p.he, MethodType)) # True
print(isinstance(Person.he, MethodType)) # True print(isinstance(p.pi, FunctionType)) # True
print(isinstance(Person.pi, FunctionType)) # True
Python 区分方法和函数的更多相关文章
- python isinstance和issubclass,区分方法和函数,反射
一.isinstance和issubclass 1.isinstance class Animal: def eat(self): print('刚睡醒吃点儿东西') class Cat(Animal ...
- python中方法与函数的区别与联系
今天huskiesir在对列表进行操作的时候,用到了sorted()函数,偶然情况下在菜鸟教程上看到了内置方法sort,同样都可以实现我对列表的排序操作,那么方法和函数有什么区别和联系呢? 如下是我个 ...
- 举例详解Python中的split()函数的使用方法
这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下 函数:sp ...
- python 一些方法函数
转Python学习笔记十一:列表(3)--列表的一些方法:http://www.cnblogs.com/dabiao/archive/2010/03/12/1683942.html python中的e ...
- python 全栈开发,Day113(方法和函数的区别,yield,反射)
一.方法和函数的区别 面向对象 初级 class StarkConfig(object): def __init__(self,model_class): self.model_class = mod ...
- Python字典内置函数和方法
Python字典内置函数和方法: 注:使用了 items.values.keys 返回的是可迭代对象,可以使用 list 转化为列表. len(字典名): 返回键的个数,即字典的长度 # len(字典 ...
- Python 用科学的方法判断函数/方法
from types import MethodType,FunctionType def check(arg): """ 检查arg是方法还是函数? :param ar ...
- Python知识之 方法与函数、偏函数、轮询和长轮询、流量削峰、乐观锁与悲观锁
方法与函数 函数需要手动传参self.cls,方法自动传,比如对象方法自动传self,类方法自动传cls,而函数相对而言需要手动传,比如静态绑定的函数,self是需要手动传值得,比如我们平常使用的函数 ...
- python encode和decode函数说明【转载】
python encode和decode函数说明 字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在p ...
随机推荐
- 谈一谈HashMap类
一.Java中的hashCode()和equals() 1. hashCode()的存在主要是用于查找的快捷性,如Hashtable,HashMap等,hashCode()是用来在散列存储结构中确定对 ...
- 安卓——animotion
在 layout下建立文件夹 animator写入动画文件xml <?xml version="1.0" encoding="utf-8"?> &l ...
- js正则、js全选、反选、全不选、ajax批删
<button onclick="fun1()">全选</button><button onclick="fun2()">全 ...
- ActiveMQ producer同步/异步发送消息
http://activemq.apache.org/async-sends.html producer发送消息有同步和异步两种模式,可以通过代码配置: ((ActiveMQConnection)co ...
- rexec/rlogin/rsh介绍
服务 是否需要密码 是否明文 功能 端口 rexec 是 是 远程执行命令 512 rlogin 是 是 远程登录得到shell 513 rsh 是 是 可远程执行命令,也可远程登录得到shell 5 ...
- jedata日期控件的开始结束日期设置
<span class="wstxt">开始日期:</span><input type="text" class="wo ...
- 部署Linux项目
部署Linux项目 1● 下载软件 ftp 安装 2● 创建连接 3● java项目 gunzip –c *.gz tar –xzf *.gz rm –rf rm -r ...
- jQuery获取select值
jQuery操作select标签 即控制select的option属性 <select id="sid" > <option value="-1&quo ...
- Linux int 最大为多大
可以查看 /usr/include/limits.h 文件 里面定义好了各种类型的最大最小值 ... /* Minimum and maximum values a `signed int' can ...
- 逆袭之旅DAY28.XIA.异常处理
2018-07-24 14:42:24 第一种: 第二种: 第三种: 执行 try--catch--finally--return(执行return 退出方法) 代码示例: 输入数字,输出对应课 ...