python-4.魔法方法

Python 魔法方法详解
http://bbs.fishc.com/thread-48793-1-1.html
(出处: 鱼C论坛)

基本魔法方法

_new_(cls [,…])返回实例化对象

>>> class CapStr(str):
        def __new__(cls,string):
            string=string.upper()
            return str.__new__(cls,string)


>>> a=CapStr('nihaoA')
>>> a
'NIHAOA'

_init_(self [,…])构造器
_del_(self)析构器

>>> class C:
        def __init__(self):
            print('__init__被调用了')
        def __del__(self):
            print('__del__被调用了')


>>> a=C()
__init__被调用了
>>> b=a
>>> del a
>>> del b
__del__被调用了

_call_(self)
任何类,只需要定义一个__call__()方法,就可以直接对实例进行调用。通过callable()函数,我们就可以判断一个对象是否是“可调用”对象。

>>> class Student():
    def __call__(self):#__call__
        print('call')


>>> s=Student()
>>> s()#可以调用
call
>>> callable(s)#可以调用
True
>>> class Student():#没有定义__call__
    pass

>>> s=Student()
>>> s()#不能调用
Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    s()
TypeError: 'Student' object is not callable
>>> callable(s)
False

_str_(self)
_repr_(self)
__str__()返回用户看到的字符串,而__repr__()返回程序开发者看到的字符串,也就是说,__repr__()是为调试服务的。

>>> class A:
        def __str__(self):
            return 'str被调用'
        def __repr__(self):
            return 'repr被调用'


>>> a=A()
>>> print(a)
str被调用
>>> a
repr被调用

通常__str__()__repr__()代码都是一样的,所以,有个偷懒的写法:
__repr__ = __str__

有关属性

_getattribute_(self,name)
_getattr_(self,name)
_setattr_(self,name,value)
_delattr_(self,name)

>>> class C:
        def __getattribute__(self,name):
            print('getattribute')
            return super().__getattribute__(name)
        def __getattr__(self,name):
            print('getattr')#不能写super().__getattr__(name),因为super对象没有这个函数
        def __setattr__(self,name,value):
            print('setattr')
            super().__setattr__(name,value)
        def __delattr__(self,name):
            print('delattr')
            super().__delattr__(name)


>>> c=C()
>>> c.x
getattribute
getattr
>>> c.x=1
setattr
>>> c.x
getattribute
1
>>> del c.x
delattr
>>> 

陷阱

>>> class Rectangle:
        def __init__(self,width=0,height=0):
            self.width=width
            self.height=height
        def __setattr__(self,name,value):
            if name=='square':
                self.width=width
                self.height=height
            else:
                self.name=value
        def getArea(self):
            return self.width*self.height


>>> r1=Rectangle(3,4)
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    r1=Rectangle(3,4)
  File "<pyshell#42>", line 3, in __init__
    self.width=width
  File "<pyshell#42>", line 10, in __setattr__
    self.name=value
  File "<pyshell#42>", line 10, in __setattr__
    self.name=value
  File "<pyshell#42>", line 10, in __setattr__
    self.name=value
  [Previous line repeated 325 more times]
  File "<pyshell#42>", line 6, in __setattr__
    if name=='square':
RecursionError: maximum recursion depth exceeded in comparison

修改

>>> class Rectangle:
        def __init__(self,width=0,height=0):
            self.width=width
            self.height=height
        def __setattr__(self,name,value):
            if name=='square':
                self.width=width
                self.height=height
            else:
                super().__setattr__(name,value)#或者self.__dict__[name]=value
        def getArea(self):
            return self.width*self.height

_get_(self,instance,owner)
* set(self,instance,value)*
_delete_(self,instance)

>>> class MyDecriptor:
        def __get__(self,instance,owner):
            print('getting...',self,instance,owner)
        def __set__(self,instance,value):
            print('getting...',self,instance,value)
        def __delete__(self,instance):
            print('getting...',self,instance)


>>> class Text:
        x=MyDecriptor()


>>> text=Text()
>>> text.x
getting... <__main__.MyDecriptor object at 0x00000000031DAF60> <__main__.Text object at 0x00000000031DA828> <class '__main__.Text'>
>>> text.x='nihao'
getting... <__main__.MyDecriptor object at 0x00000000031DAF60> <__main__.Text object at 0x00000000031DA828> nihao
>>> del text.x
getting... <__main__.MyDecriptor object at 0x00000000031DAF60> <__main__.Text object at 0x00000000031DA828>

算数运算

>>> class New_int(int):
        def __add__(self,other):
            return int.__sub__(self,other)
        def __sub__(self,other):
            return int.__add__(self,other)


>>> a=New_int(3)
>>> b=New_int(5)
>>> a+b
-2
>>> a-b
8

反运算

当左侧操作符不支持相应的操作时

>>> class Nint(int):
        def __radd__(self,other):
            return int.__sub__(self,other)


>>> a = Nint(5)
>>> b = Nint(3)
>>> a+b
8
>>> 1+b
2
>>> class Nint(int):
        def __rsub__(self,other):
            return int.__sub__(self,other)


>>> a = Nint(5)
>>> 3-a
2#发现错误
>>> class Nint(int):#重写
        def __rsub__(self,other):
            return int.__sub__(other,self)


>>> a = Nint(5)
>>> 3-a
-2#正确
>>> 

增量赋值运算

_iadd_(self,other)

一元操作符

容器类型

定制容器

定制不变的容器时,定义
_len_(self)
_getitem_(self,key)
定制可变的容器时,还要定义
_setitem_(self,key,value)
_delitem_(self,value)

迭代器

_iter_(self)
_next_(self)

>>> c1.dict
{0: 0, 1: 3, 2: 0, 3: 0, 4: 0}
>>> it=iter('nihaoa')
>>> next(it)
'n'
>>> next(it)
'i'
>>> next(it)
'h'
>>> while True:#用while也可以实现for
    try:
        each=next(it)
    except StopIteration:
        break
    print(each)


a
o
a
>>> for it in 'nihao':
    print(it)


n
i
h
a
o
>>> 

如果一个类想被用于for … in循环,类似list或tuple那样,就必须实现一个__iter__()方法,该方法返回一个迭代对象,然后,Python的for循环就会不断调用该迭代对象的__next__()方法拿到循环的下一个值,直到遇到StopIteration错误时退出循环。

>>> class Fibs:
        def __init__(self,n=10):
            self.a=0
            self.b=1
            self.n=n
        def __iter__(self):#实例本身就是迭代对象,故返回自己
            return self
        def __next__(self):#
            self.a,self.b=self.b,self.a+self.b
            if self.a>self.n:
                raise StopIteration
            return self.a


>>> fibs=Fibs()
>>> for each in fibs:   
        print(each)

1
1
2
3
5
8
13
21
34
55
89
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值