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__ 方法的更多相关文章

  1. Python中的__new__()方法与实例化

    @Python中的__new__()方法与实例化   __new__()是在新式类中新出现的方法,它作用在构造方法建造实例之前,可以这么理解,在Python 中 存在于类里面的构造方法__init__ ...

  2. python中的replace()方法的使用

    python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...

  3. Python中的字符串方法

    Python中的字符串方法 字符串类即str提供了许多有用的方法来操纵字符串.具体来说,我们将讨论如下的方法. 搜索字符串内的子字符串. 测试字符串. 格式字符串. 转换字符串. 回顾前面的章节,方法 ...

  4. python中的sort方法

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...

  5. python中的sort方法使用详解

    Python中的sort()方法用于数组排序,本文以实例形式对此加以详细说明: 一.基本形式 列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不 ...

  6. Python中格式化format()方法详解

    Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...

  7. 关于python中的特殊方法

    研究了几个小时,大概对python中的特殊方法一知半解,现在写写自己的理解,以及记录一些找到的资源.待自己有比较深入理解的时候,再来更新 https://docs.python.org/3/refer ...

  8. Python中使用item()方法遍历字典的例子

    Python中使用item()方法遍历字典的例子 这篇文章主要介绍了Python中使用item()方法遍历字典的例子,for...in这种是Python中最常用的遍历字典的方法了,需要的朋友可以参考下 ...

  9. Python中的open()方法总结

    总结Python中的open()方法 message= {'企业即时通信': 'aaa', '企业名称': 'bbb'} with open("..\\r.txt", " ...

随机推荐

  1. HTML_3

    html列表 有序列表:在网页上定义一个有编号的内容列表可以用<ol>.<li>配合使用来实现,在网页上生成的列表,每条项目上按1.2.3编号,有序列表在实际开发中较少使用.代 ...

  2. CPP-网络/通信:用CMarkup类操纵XML

      首先到http://www.firstobject.com/下载CMarkup教学版,解压后里面是一个DEMO,将Markup.h .cpp拷贝并添加到工程中,第一次编译可能会出现预编译错误,解决 ...

  3. 利用Vue.js实现登录/登出以及JWT认证

    JSON Web Token 入门教程:http://www.ruanyifeng.com/blog/2018/07/json_web_token-tutorial.html 后端代码地址:https ...

  4. PayPal为什么从Java迁移到Node.js 性能提高一倍 文件代码减少44%

    大家都知道PayPal是另一家迁移到Node.js平台的大型公司,Jeff Harrell的这篇博文 Node.js at PayPal  解释了为什么从Java迁移出来的原因: 开发效率提高一倍(2 ...

  5. UpdatePanel中点击按钮Session过期跳转页面相关问题:Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息

    使用 Response.Write("<script language=javascript>window.location.href='Login.aspx';</scr ...

  6. 课下作业04-2String的使用方法

    1.动手动脑之String.equals()方法public class StringEquals { public static void main(String[] args) { String ...

  7. Android驱动开发读书笔记五

    第五章 本章介绍了S3C6410开发板的功能,开发板的不同主要是在烧录嵌入式系统的方式不同,以及如何在此开发板上安装Android. 1.安装串口调试工具minicom 首先需要一根USB转串口线,由 ...

  8. 【Python学习之五】高级特性2(切片、迭代、列表生成器、生成器、迭代器)

    2.迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration).在Python中,迭代是通过for ... in来完成的. ...

  9. overtrue/wechat 包 由 sys_get_temp_dir 引发的 the directory "c:\Windows" is not writable

    vendor\overtrue\wechat\src\Foundation\Application.php registerBase 方法 在初始化属性时 $this['cache'] = funct ...

  10. MySQL中文转换成拼音的函数

    CREATE DEFINER=`root`@`localhost` FUNCTION `fristPinyin`(`P_NAME` VARCHAR(255) CHARSET utf8) RETURNS ...