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", " ...
随机推荐
- 自行实现一个简易RPC框架
10分钟写一个RPC框架 1.RpcFramework package com.alibaba.study.rpc.framework; import java.io.ObjectInputStrea ...
- Python学习记录4(语句)
赋值语句 序列解包 条件语句 语句块 布尔变量 条件执行和if语句 条件运算符 循环 while语句 for循环 迭代工具 跳出循环 break continue while truebreak语句 ...
- poj1654 Area
题目描述: vjudge POJ 题解: 本以为是水题结果是神题 计算几何求多边形面积. 考虑到结果一定是整数或者整数/2,我们应该用long long 来存…… 用double会死…… 还有日常只能 ...
- NodeJS基础入门-Buffer
Buffer.byteLength console.log(Buffer.byteLength('test')); console.log(Buffer.byteLength('我是C语言爱好者')) ...
- ThinkPHP5 高级查询之构建分组条件
ThinkPHP5 高级查询之构建分组条件 一.在tp5中通过where方法如何构建分组条件, 例如:where user_id=$this->user_id and (status in (4 ...
- 【android】【android studio】修改emulator的本地化环境
Changing the emulator locale from the adb shell To change the locale in the emulator by using the ad ...
- 补之前 如何改变jupyter打开文件的路径
目录 如何改变jupyter打开文件的路径 第一种方法: 第二种方法 第三种方法 如何改变jupyter打开文件的路径 当我们直接打开jupyter时,直接加载的是我们的C盘文件 现在我们想打开其他盘 ...
- LightOj:1422-Halloween Costumes
传送门:http://www.lightoj.com/volume_showproblem.php?problem=1422 Halloween Costumes problem descriptio ...
- CSS动画小结
CSS动画 原理:1.画面之间变化 2.视觉暂留作用 常见问题 1.CSS 动画的实现方式有几种 1.transition 2. keyframes(animation) 2.过渡动画和关键帧动画 ...
- String的getBytes()方法
这是一篇转帖: 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/maxracer/archive/2010/12/14/6075057.aspx 在Java中,Stri ...