zoukankan      html  css  js  c++  java
  • 【Pyton】【小甲鱼】魔法方法

    1.__init__

     1 >>> class Rectangle:
     2     def __init__(self,x,y):
     3         self.x=x
     4         self.y=y
     5     def getPeri(self):
     6         return(self.x+self.y)*2
     7     def getArea(self):
     8         return self.x*self.y
     9 
    10     
    11 >>> rect=Rectangle(3,4)
    12 >>> rect.getPeri()
    13 14
    14 >>> rect.getArea()
    15 12
    16 >>> class A:
    17     def __init__(self):
    18         return "A fo A-Cup"
    19 
    20     
    21 >>> a=A() #由于定义了A有返回值了,所以会报错。init无返回值
    22 Traceback (most recent call last):
    23   File "<pyshell#37>", line 1, in <module>
    24     a=A()
    25 TypeError: __init__() should return None, not 'str'

    2.__new__(cls[,])

    1 >>> class CapStr(str):
    2     def __new__(cls,string): #传入str化身为string
    3         string=string.upper() #string转换为为大写字符串
    4         return str.__new__(cls,string)#返回的时候必须重写new方法,否则则会自动调用capstr函数
    5 >>> a=CapStr("I love fishC.com!")
    6 >>> a
    7 'I LOVE FISHC.COM!'

    3.__del__(self):当对象将要被销毁的时候,此方法就会被调用

     1 >>> class C:
     2     def __init__(self):
     3         print("我是__init__方法,我被调用了...")
     4     def __del__(self):
     5         print("我是__del__方法,我被调用了...")
     6     
     7 >>> c1=C()
     8 我是__init__方法,我被调用了...
     9 >>> c2=c1
    10 >>> c3=c2
    11 >>> del c3
    12 >>> del c2
    13 >>> del c1 #只有删除了所有引用方法C的对象后,才启动垃圾回收机制,垃圾回收机制在销毁对象的时候,会自动调用del方法,对其中的内容进行垃圾回收,才会打印出来回收的内容。
    14 我是__del__方法,我被调用了...

     二、算数运算

    1.因为python万物皆对象,所以对象中的内容也是可以相加的。

     1 >>> type(len)
     2 <class 'builtin_function_or_method'>
     3 >>> type(dir)
     4 <class 'builtin_function_or_method'>
     5 >>> type(int)
     6 <class 'type'>
     7 >>> type(list)
     8 <class 'type'>
     9 >>> class C:
    10     pass
    11 
    12 >>> type(C)
    13 <class 'type'>
    14 >>> a=int('123')
    15 >>> a
    16 123
    17 >>> b=int('456')
    18 >>> a+b
    19 579

    2.魔法函数实例

     1 >>> class New_int(int):
     2     def __add__(self,other):
     3         return int.__sub__(self,other)
     4     def __sub__(self,other):
     5         return int.__add__(self,other)
     6 
     7     
     8 >>> a=New_int(3)
     9 >>> b=New_int(5)
    10 >>> a+b #a+b自动调用__add__方法中的
    11 -2
    12 >>> a-b
    13 8
    14 >>> class Try_int(int):
    15     def __add__(self,other):
    16         return self+other 
    17     def __sub__(self,other):
    18         return self-other
    19 
    20     
    21 >>> a=Try_int(3)
    22 >>> b=Try_int(5)
    23 >>> a+b
    24 下方一连串报错。原因是,a+b,调用__add__函数,self绑定a进来,other代表b。而相当于加法,又进来__add__方法,进来有是加法,然后又递归。
    25

    #下面让a+b后不报错 26 >>> class Try_int(int): 27 def __add__(self,other): 28 return int(self)+int(other) 29 def __sub__(self,other): 30 return int(self)-int(other) 31 32 33 >>> b=Try_int(5) 34 >>> a=Try_int(3) 35 >>> a+b 36 8 #由于上面定义的__add__方法中返回一个整型,__sub__方法中返回一个整型,所以可以相加成一个整型数据。

    3.魔法函数含义:

    4.divmod(a,b):返回的值是一个元祖:(a//b,a%b)

    5.__lshift__(self,other):按位左移。这个魔法方法代表的数是二进制数组成的,例如3==00000011,按位左移一位就是00000110

    三、反运算符

     1 #__add__方法前带r,代表add的反运算符,例如 a+b,处于add两项中的前一项a为主动一方,b为被动一方,所以当add前者a有值的时候,直接进入add运算,如果a娶不到值的时候执行反运算,也就是radd中的函数。
     2 class Nint(int):
     3     def __radd__(self,other):
     4         return int.__sub__(self,other)
     5 
     6     
     7 >>> a=Nint(3)
     8 >>> b=Nint(5)
     9 >>> a+b
    10 8
    11 >>> 1+b #执行反运算(sub中self取的b值,other取 的1 5-1=4)
    12 4
     1 >>> 1-b------#思考为什么=-4
     2 -4
     3 >>> 1+b
     4 4
     5 >>> class Nint(int):
     6     def __radd__(self,other):
     7         return int.__sub__(other,self) #括号中的顺序为减法顺序other-self
     8     
     9 >>> b=Nint(5)
    10 >>> 1+b(self代表b=5,other代表1,1-5=-411 -4
    12 #增量赋值运算
    13 >>> a=a+b
    #一元操作符
    参照书611页
  • 相关阅读:
    Minio对象存储
    白话解说TCP/IP协议三次握手和四次挥手
    企业环境下MySQL5.5调优
    Mac下iTerm2配置lrzsz功能
    七牛云图床和Markdown使用
    SSIS: 把存储在数据库中的图片导出来
    关闭Outlook的时候使之最小化
    【转】CTE(公用表表达式)
    通过SSIS监控远程服务器磁盘空间并发送邮件报警
    在Windows Server 2008 R2 中架设 SMTP 服务器
  • 原文地址:https://www.cnblogs.com/zhuzhubaoya/p/6580678.html
Copyright © 2011-2022 走看看