鱼C论坛

 找回密码
 立即注册
查看: 1535|回复: 0

[学习笔记] A-19-魔法方法

[复制链接]
发表于 2018-7-18 20:51:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #coding=UTF-8

  2. #知识点总结
  3. """
  4. 1.魔法方法详解 http://bbs.fishc.org/thread-48793-1-1.html
  5. 2.__new__(cls [, ...]): 是一个对象实例化时所调用的第一个方法, 第一个参数是类(cls), 其他参数会直接传递给__init__方法
  6.   __new__ 返回实例对象, 通常是参数cls这个参数的实例化对象, 也可以返回其他对象.

  7. 3. __init__(self [, ...]): 当实例对象有明确的初始化步骤时改写__init__()
  8. 4. __del__(self): 当对象将被销毁的时候调用, 销毁指的是垃圾回收机制回收这个对象

  9. 5.python3以后, int(), float(), list(), str()等工厂函数的调用实际上就是创建类的实例化对象的过程, 工厂函数就是类对象

  10. 6.当对实例对象进行算术运算时, 会自动调用算术运算的魔法方法

  11.       运算符                   对应的魔法方法
  12.         +                __add__(self, other)
  13.         -                __sub__(self, other)
  14.         *                __mul__(self. other)
  15.         /                __truediv__(self, other)
  16.         //               __floordiv__(slef, other)
  17.         %                __mod__(self, other)
  18.         **               __pow__(self, other[, modulo])
  19.         <<               __lshift__(self, other)
  20.         >>               __rshift__(slef, other)
  21.         &                __and__(self, other)
  22.         ^                __xor__(self, other)
  23.         |                __or__(self, other)


  24.     例如, 当 a + b 时, 会自动调用 __add__() 方法, 可以通过修改魔法方法来自定义算术运算
  25. """


  26. #1.
  27. class CapStr(str):   #定义一个继承自str的类
  28.     def __new__(cls,string):
  29.         string = srting.upper()
  30.         return str.__new__(cls,string)
  31. a=CapStr("I love you")
  32. print(a)

  33. #2.
  34. class Rectangle:
  35.     def __init__(self,x,y):  #重新定义初始化函数
  36.         self.x=x
  37.         self.y=y
  38.     def getperi(self):
  39.         return (self.x+self.y)*2
  40.     def getarea(self):
  41.         return self.x*self.y

  42. rect=Rectangle(3,4)
  43. print(rect.getperi())
  44. print(rect.getarea())

  45. #3.
  46. class C:
  47.     def __init__(self):
  48.         print("我是_init_方法,我被调用了....")
  49.     def __del__(self):
  50.         print("我是_del_方法,我被调用了....")
  51. c1=C()
  52. c2=c1
  53. c3=c2
  54. del c3
  55. del c2
  56. del c1

  57. #4.
  58. class Try_int(int):
  59.     def __add__(self, other):
  60.         return int (self) + int(other)
  61.     def __sub__(self, other):
  62.         return  int(self) - int(other)

  63. a=Try_int(3)
  64. b=Try_int(5)
  65. print(a+b)

  66. #5.
  67. class  Nint(int):
  68.     def  __new__(cls, arg = 0):
  69.         if  type(arg) == type(str):
  70.             total = 0
  71.             for each_str in arg:
  72.                total += ord(each_str)

  73.             arg = total
  74.         return int.__new__(cls, arg)


  75. #6. 反运算的魔法方法,当左操作数不支持相应操作时被调用
  76. #  例如__radd__, a + b, 当a对象没有__add__方法时, b对象才会调用__radd__:
  77. class Nint(int):
  78.     def __radd__(self, other):
  79.         print("__radd__ 被调用了!")
  80.         return int.__sub__(self, other)

  81. a = Nint(5)
  82. b = Nint(3)
  83. print(a+b)
  84. print(1 + b) #调用__radd__(self, other),其中self为b,other为1
  85. print(3-a)   #这是自动调用的是_sub_函数,即__sub__(3, a)

  86. class Nint(int):
  87.     def __rsub__(self, other):
  88.         return int.__sub__(self,other)
  89. a=Nint(5)
  90. print(3-a)    #调用函数__rsub__(self, other),其中self为a


  91. #


  92. """
  93. #在继承的类中调用基类的方法, 使用super()

  94. #对于修饰符@staticmethod, 表示紧跟的一个方法为静态方法, 静态方法不会绑定到实例对象上, 可以节省开销,
  95. #  所以定义静态方法时不需要传入self参数, 当对象访问时也不会传入self参数, 例如:

  96. class C:
  97.         @staticmethod  # 该修饰符表示 static() 是静态方法
  98.         def static(arg1, arg2, arg3):
  99.                 print(arg1, arg2, arg3, arg1 + arg2 + arg3)

  100.         def nostatic(self):
  101.                 print("I'm the f**king normal method!")
  102. c1 = C()
  103. c2 = C()
  104. # 静态方法只在内存中生成一个,节省开销
  105. print(c1.static is C.static)
  106. print(c1.nostatic is C.nostatic)
  107. print(c1.static)
  108. print(c2.static)
  109. print(C.static)

  110. # 普通方法每个实例对象都拥有独立的一个,开销较大
  111. print(c1.nostatic)
  112. print(c2.nostatic)
  113. print(C.nostatic)
  114. print(c1.static(1, 2, 3))
  115. print(C.static(1, 2, 3))

  116. """
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-21 00:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表