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 ...
随机推荐
- fdisk分区和挂载
查看硬盘情况 fdisk -l 修改分区类型为LVM sudo fdisk /dev/sda (/dev/sda为对应的设备名,也可能是其它名字) 按n新建分区 按p选择主分区 按t改变分区的syst ...
- grid的简单使用
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>grid ...
- leetcode-algorithms-18 4Sum
leetcode-algorithms-18 4Sum Given an array nums of n integers and an integer target, are there eleme ...
- Farm Irrigation(非常有意思的并查集)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- 《高性能SQL调优精要与案例解析》一书谈SQL调优(SQL TUNING或SQL优化)学习
<高性能SQL调优精要与案例解析>一书上市发售以来,很多热心读者就该书内容及一些具体问题提出了疑问,因读者众多外加本人日常工作的繁忙 ,在这里就SQL调优学习进行讨论并对热点问题统一作答. ...
- Python3+pdfminer+jieba+wordcloud+matplotlib生成词云(以深圳十三五规划纲要为例)
一.各库功能说明 pdfminer----用于读取pdf文件的内容,python3安装pdfminer3k jieba----用于中文分词 wordcloud----用于生成词云 matplotlib ...
- sudo配置教程
一.相关说明 1.sudo配置文件是/etc/sudoers:另外会自动包含/etc/sudoers.d目录下的文件(/etc/sudoers文件最后有一句“#includedir /etc/sudo ...
- AIX 5335端口IBM WebSphere应用服务器关闭连接信息泄露漏洞的修复
今天按要求协助进行漏洞修复,有个“IBM WebSphere应用服务器关闭连接信息泄露漏洞”,一直没太搞清是不是没打补丁引起的问题. 感觉同样的安装有的报这漏洞有的不报,而报的有的是应用端口,有时是控 ...
- 常用的flex知识 ,比起float position 好用不少
flex布局具有便捷.灵活的特点,熟练的运用flex布局能解决大部分布局问题,这里对一些常用布局场景做一些总结. web页面布局(topbar + main + footbar) 示例代码 要 ...
- django自定义标签,int转化为str类型
1.在app中创建templatetags目录,目录名必须为templatetags 2.在目录templatetags中创建一个.py文件,例如 strFilter.py strFilter.py ...