zoukankan      html  css  js  c++  java
  • Python面向对象(特殊成员)

    day25

        __init__     类()自动执行
        __del__
        __call__     对象()  类()() 自动执行
        __int__      int(对象)  
        __str__      str()

    特殊成员

     1 class Foo:
     2     def __init__(self):
     3         print('init')
     4 
     5     def __call__(self, *argc, **kwarge):
     6         print('call')
     7 
     8 obj = Foo()#直接执行__init__
     9 
    10 obj()#执行__call__中内容
    11 
    12 Foo()()#同上

    对象加括号,obj()执行__call__()。

    执行结果:

    init
    call
    init
    call
    
    Process finished with exit code 0

    __str__,__int__

     1 class Foo:
     2     def __init__(self):
     3         pass
     4 
     5     def __int__(self):
     6         return 111
     7 
     8     def __str__(self):
     9         return "nizhipeng"
    10 obj = Foo()
    11 
    12 print(obj, type(obj))
    13 
    14 r = int(obj)#自动执行__int__方法,并将返回值给int对象
    15 print(r)
    16 
    17 print(str(obj))

    类型转换的时候,自动执行__int__,和__str__方法。

    执行结果:

    nizhipeng <class '__main__.Foo'>
    111
    nizhipeng
    
    Process finished with exit code 0

    自动转换

    1 class Foo:
    2     def __init__(self, n, a):
    3         self.name = n
    4         self.age = a
    5     def __str__(self):
    6         return '%s-%s' %(self.name, self.age)
    7 
    8 obj = Foo('alex', 18)
    9 print(obj) #obj自动转成了str类型

    第9行自动执行了str(obj)。

    执行结果:

    alex-18
    
    Process finished with exit code 0

    __add__

     1 class Foo:
     2 
     3     def __init__(self, name, age):
     4         self.name = name
     5         self.age = age
     6 
     7     def __add__(self, other):
     8         #self为obj1包含信息(alex,18)
     9         #other为obj2包含信息(eiro,66)
    10 
    11         '''return self.age + other.age'''
    12         return Foo(obj1.name, other.age)#对象1的名字,对象2的年龄
    13 
    14     def __del__(self):
    15         print("析构方法")#对象销毁时执行
    16 
    17 
    18 obj1 = Foo('alex', 19)
    19 obj2 = Foo('eiro', 66)
    20 
    21 r = obj1 + obj2#两个对象相加时,自动执行第一个对象的__add__方法,并且将第二个对象当作参数
    22 
    23 print(r.age, type(r))
    两个对象相加时,自动执行第一个对象的__add__方法,并且将第二个对象当作参数
    执行结果:
    66 <class '__main__.Foo'>
    析构方法
    析构方法
    析构方法
    
    Process finished with exit code 0
    __dict__
     1 class Foo:
     2 
     3     '''
     4     注释也是类成员
     5     '''
     6     def __init__(self, name, age):
     7         self.name = name
     8         self.age = age
     9         self.n = 123
    10 
    11 obj = Foo('alex', 18)
    12 
    13 d = obj.__dict__#通过字典形式显示对象成员
    14 print(d)
    15 
    16 ret = Foo.__dict__#通过字典形式显示类成员
    17 print(ret)

    __dict__()可以通过字典显示类成员,和对象成员。注释也是类成员的一部分。

    执行结果:

    {'name': 'alex', 'age': 18, 'n': 123}
    {'__init__': <function Foo.__init__ at 0x7fb7f3155620>, '__doc__': '
        注释也是类成员
        ', '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__module__': '__main__'}
    44
    
    Process finished with exit code 0

    __getitem__,__setitem__,__delitem__

     1 class Foo:
     2     def __init__(self, name, age):
     3         self.name = name
     4         self.age = age
     5 
     6     def __getitem__(self, item):
     7         return item+10
     8 
     9     def __setitem__(self, key, value):
    10         print(key, value)
    11 
    12     def __delitem__(self, key):
    13         print(key)
    14 li = Foo('alex', 18)
    15 #以索引方式访问对象,执行__getitem__方法
    16 
    17 r = li[8]#自动执行li对象类中的__getitem__方法, 8作为参数给item
    18 print(r)#有获取数据属于需要返回值
    19 
    20 li[100] = 'aasd'#执行__setitem__(), key为100,value为aasd
    21 
    22 del li[999]#执行__delitem__()

    以索引方式访问需要以上几种特殊方法。

    __setitem__,__delitem__并不获取数据,不需要return。

    执行结果:

    18
    100 aasd
    999
    
    Process finished with exit code 0

    __iter__

     1 class Foo:
     2     def __init__(self, name, age):
     3         self.name = name
     4         self.age = age
     5 
     6     def __iter__(self):
     7         return iter([11,22,33])#生成迭代器对象
     8 #如果类中有__iter__方法,对象为可迭代对象
     9 #对象.__iter__()的返回值:迭代器
    10 li = Foo('alex', 18)
    11 
    12 
    13 for i in li:
    14     print(i)
    如果类中有__iter__方法,对象为可迭代对象,iter()生成迭代对象。

    执行结果:

    11
    22
    33
    
    Process finished with exit code 0

    __new__ 和 __metaclass__

    Foo类其实为type类的对象。

    所有类默认继承object类。

    http://www.cnblogs.com/wupeiqi/p/4766801.html

    其中第11节

     1 class MyType(type):
     2     #self 为 Foo
     3     def __init__(self, *args, **kwargs):
     4         print('123')
     5         pass
     6 
     7     def __call__(cls):
     8         print('456')
     9 
    10 #Foo为MyType对象
    11 class Foo(object,metaclass=MyType):#任何类都继承object类,并使Foo类为type类子类MyType的对象
    12     def __init__(self):
    13         print('567')
    14 
    15     def func(self):
    16         print('hello')
    17 
    18 #使Foo类为type类子类MyType的对象,创建Foo对象时会执行类中的__init__()方法
    19 #输出  123
    20 
    21 #有__call__方法时输出456,对象加(),执行__call__方法,Foo是MyType的一个对象。
    22 #没有时输出567,Foo类创建obj对象,执行__init__方法。
    23 obj = Foo()
    任何类都继承object类,并使Foo类为type类子类MyType的对象
    执行结果:
    123
    456
    
    Process finished with exit code 0
  • 相关阅读:
    Dedecms(织梦)文章内容页和图片集内容页,调用缩略图的方法
    如何修改织梦dedecms文章标题的最大长度
    织梦自定义表单后台管理增加全选功能,批量删除垃圾留言信息
    自定义表单SQL命令行批量删除垃圾留言
    织梦后台卡死的原因分析及开关功能解决办法
    织梦网站迁移的几种方法
    Dedecms(织梦)文章内容页和图片集内容页,调用缩略图的方法
    织梦CMS被挂马特征汇总
    DedeCMS模板中用彩色tag做彩色关键词
    HDU6038
  • 原文地址:https://www.cnblogs.com/112358nizhipeng/p/9835980.html
Copyright © 2011-2022 走看看