python中的 __xxx__ 方法
1 __class__
instance.__class__
The class to which a class instance belongs
def foo():
pass
class A(object):
pass
s = 'abc'
l = [,,]
t = (,,)
d = {'a':} print('foo',foo.__class__)
print('foo',foo.__class__.__class__)
print('A',A.__class__)
print('A',A.__class__.__class__)
print('s',s.__class__)
print('s',s.__class__.__class__)
print('l',l.__class__)
print('l',l.__class__.__class__)
print('t',t.__class__)
print('t',t.__class__.__class__)
print('d',d.__class__)
print('d',d.__class__.__class__)
输出:
foo <class 'function'>
foo <class 'type'>
A <class 'type'>
A <class 'type'>
s <class 'str'>
s <class 'type'>
l <class 'list'>
l <class 'type'>
t <class 'tuple'>
t <class 'type'>
d <class 'dict'>
d <class 'type'>
2 __name__
definition.__name__
The name of the class, function, method, descriptor, or generator instance.
def foo():
pass
class A(object):
pass
s = 'abc'
l = [,,]
t = (,,)
d = {'a':} print(foo.__name__)
print(foo.__class__.__name__)
print(foo.__class__.__class__.__name__)
print(A.__name__)
print(A.__class__.__name__)
print(A.__class__.__class__.__name__)
输出:
foo
function
type
A
type
type
3 __call__
object.__call__(self,[ args ** ])
Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, ...) is a shorthand for x.__call__(arg1, arg2, ...).
Python中的函数是一级对象。这意味着Python中的函数的引用可以作为输入传递到其他的函数/方法中,并在其中被执行。
而Python中类的实例(对象)可以被当做函数对待。也就是说,我们可以将它们作为输入传递到其他的函数/方法中并调用他们,正如我们调用一个正常的函数那样。而类中__call__()函数的意义正在于此。为了将一个类实例当做函数调用,我们需要在类中实现__call__()方法。也就是我们要在类中实现如下方法:def __call__(self, *args)。这个方法接受一定数量的变量作为输入。
假设x是X类的一个实例。那么调用x.__call__(1,2)等同于调用x(1,2)。这个实例本身在这里相当于一个函数。
class A(object):
def __init__(self,name,age):
self.name = name
self.age = age
def __call__(self):
print('{}年龄{}'.format(self.name,self.age)) a = A('zuo',)
a() class B(object):
def __init__(self):
pass
def __call__(self,a,b):
self.a = a
self.b = b
print('{}*{}={}'.format(a,b,a*b)) b = B()
b(,)
b.__call__(,)
python中的 __xxx__ 方法的更多相关文章
- Python中的__new__()方法与实例化
@Python中的__new__()方法与实例化 __new__()是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在Python 中 存在于类里面的构造方法__init__ ...
- python中的replace()方法的使用
python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...
- Python中的字符串方法
Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...
- python中的sort方法
Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...
- python中的sort方法使用详解
Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...
- Python中格式化format()方法详解
Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...
- 关于python中的特殊方法
研究了几个小时,大概对python中的特殊方法一知半解,现在写写自己的理解,以及记录一些找到的资源.待自己有比较深入理解的时候,再来更新 https://docs.python.org/3/refer ...
- Python中使用item()方法遍历字典的例子
Python中使用item()方法遍历字典的例子 这篇文章主要介绍了Python中使用item()方法遍历字典的例子,for...in这种是Python中最常用的遍历字典的方法了,需要的朋友可以参考下 ...
- Python中的open()方法总结
总结Python中的open()方法 message= {'企业即时通信': 'aaa', '企业名称': 'bbb'} with open("..\\r.txt", " ...
随机推荐
- kvm笔记
1 virt-manager安装虚拟机无法使用键盘解决 今天远程用VNC登录服务器安装虚拟机,结果使用virt-manager安装虚拟机后在初始阶段无法使用键盘设置,这不雪崩了,后来来回试,找到了原因 ...
- pandas的数据联级
一.索引的堆(stack) 1.行列的转化: Stack():列转行 Unstack():行转列 Stack对应行, 使用小技巧:使用stack()的时候,level等于哪一个,哪一个就消失,出现在行 ...
- centos7重启后/etc/resolv.conf 被还原解决办法
每次重启服务器后,/etc/resolv.conf文件就被自动还原了,最后发现是被Network Manager修改了. 查看Network Manager服务状态 systemctl status ...
- 如何使用jmeter做接口测试
1.传参:key=value形式 2.传参:json格式 3.jmeter上传文件 4.jmeter传cookie 或者使用 HTTP Cookie管理器
- 有关git clone 下载速度变慢的解决方法
使用提示:请注意一下,以下方法是在搭有梯子的情况下进行的,也就是说在有梯子的情况下,下载速度始终很慢,使用了以下方法用梯子下载达到正常速度,并没有尝试修复过后不用梯子下载. 所以,如果使用了以下方法, ...
- 20181206(re,正则表达式,哈希)
1.re&正则表达式 2.hashlib 一:re模块&正则表达式 正则:正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描 ...
- 水题:51Nod1432-独木舟
1432 独木舟 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 Problem Description n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两 ...
- German Collegiate Programming Contest 2018 B. Battle Royale
Battle Royale games are the current trend in video games and Gamers Concealed Punching Circles (GCPC ...
- Linux学习-什么是 X Window System
Unix Like 操作系统不是只能进行服务器的架设而已,在美编.排版.制图.多媒体应用上也是有其 需要的. 这些需求都需要用到图形接口 (Graphical User Interface, GUI) ...
- jenkins匿名用户登录 - 安全设置
最近自己安装配置jenkins,但是跑任务时,发现是匿名账户登录,且提示: 后来发现搭建好jenkins之后,默认就是匿名用户登录的,可以在安装设置菜单里进行账户管理. 1.登录Jenkins服务器, ...