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 ...
随机推荐
- 漏洞复现——bash远程解析命令执行漏洞
漏洞描述:Bash脚本在解析某些特殊字符串时出现逻辑错误导致可以执行后面的命令,在一些cgi脚本中,数据是通过环境变量来传递的,这样就会形成该漏洞 漏洞原理:bash通过以函数名作为环境变量名,以“( ...
- centos7 keepalived+nginx实现vip漂移高可用
一.Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案, Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前 ...
- vue项目 sockjs-node一直报错问题
vue3下 vue.config.js中 devServer: { host: '0.0.0.0', port: 8080, proxy: { '/': { target: 'http://127.0 ...
- Grafana安装配置介绍
一.Grafana介绍 Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.zabbix.InfluxDB.Pr ...
- python的数据类型--list和tuple
list是Python的一种数据类型,是一个有序的集合,可以随时添加和删除.写法为list名称+[] list[]内的元素不仅仅为str,可以是数字,布尔值. 访问方法为变量名或者变量[索引],和C的 ...
- 记录一下ES6扩展运算符(三点运算符)...的用法
...运算符用于操作数组,有两种层面 1. 第一个叫做 展开运算符(spread operator),作用是和字面意思一样,就是把东西展开.可以用在array和object上都行. 比如: let a ...
- weblogic查看版本号教程
1.查看软件版本号 cd /weblogic/bea/wlserver_10.3/server/lib java -cp weblogic.jar weblogic.version 说明:版本号后边的 ...
- selinux介绍/状态查看/开启/关闭
SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的实现,是 Linux历史上最杰出的新安全子系统--百度百科. 基于经验来说SELinux在 ...
- swiper添加了自动滚动效果,然后用手指划过页面,发现自动滚动效果不生效了
我给swiper添加了自动滚动效果,然后用手指划过页面,发现自动滚动效果不生效了,哪里出了问题呢? 添加参数 autoplayDisableOnInteraction : false,
- Guidelines for Writing a Good NIPS Paper
By the NIPS 2006 Program Committee With input from Andrew Ng, Peter Dayan, Daphne Koller, Sebastian ...