C++拷貝建構函式,解構函式與記憶體洩漏的那些坑 阿新 • • 發佈:2020-12-07 針對 __str__ 方法給出一個比較直觀的例子: class people: def __init__(self,name,age): self.name=name self.age=age def __str__(self): return '這個人的名字是%s,已經有%d歲了!'%(self.name,self.age) a=people('孫悟空',999) print(a) 輸出: 這個人的名字是孫悟空,已經有999歲了! 如果沒有過載函式的話輸出的就是一串看不懂的字串: <__main__.people object at 0x00000272A730D278> 最新的 Python3.7 中(2018.07.13),對類的建構函式進行了精簡。 3.7 版本: from dataclasses import dataclass @dataclass class A: x:int y:int def add(self): return self.x + self.y 相當於以前的: class A: def __init__(self,x,y): self.x = x self.y = y def add(self): return self.x + self.y Python3 中類的靜態方法、普通方法、類方法 靜態方法: 用 @staticmethod 裝飾的不帶 self 引數的方法叫做靜態方法,類的靜態方法可以沒有引數,可以直接使用類名呼叫。 普通方法: 預設有個self引數,且只能被物件呼叫。 類方法: 預設有個 cls 引數,可以被類和物件呼叫,需要加上 @classmethod 裝飾器。 class Classname: @staticmethod def fun(): print('靜態方法') @classmethod def a(cls): print('類方法') # 普通方法 def b(self): print('普通方法') Classname.fun() Classname.a() C = Classname() C.fun() C.a() C.b() 反向運算子過載: __radd__:加運算 __rsub__:減運算 __rmul__:乘運算 __rdiv__:除運算 __rmod__:求餘運算 __rpow__:乘方 複合過載運算子: __iadd__:加運算 __isub__:減運算 __imul__:乘運算 __idiv__:除運算 __imod__:求餘運算 __ipow__:乘方 運算子過載的時候: #!/usr/bin/python3 class Vector: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return 'Vector (%d, %d)' % (self.a, self.b) def __repr__(self): return 'Vector (%d, %d)' % (self.a, self.b) def __add__(self,other): if other.__class__ is Vector: return Vector(self.a + other.a, self.b + other.b) elif other.__class__ is int: return Vector(self.a+other,self.b) def __radd__(self,other): """反向算術運算子的過載 __add__運算子過載可以保證V+int的情況下不會報錯,但是反過來int+V就會報錯,通過反向運算子過載可以解決此問題 """ if other.__class__ is int or other.__class__ is float: return Vector(self.a+other,self.b) else: raise ValueError("值錯誤") def __iadd__(self,other): """複合賦值算數運算子的過載 主要用於列表,例如L1+=L2,預設情況下呼叫__add__,會生成一個新的列表, 當資料過大的時候會影響效率,而此函式可以過載+=,使L2直接增加到L1後面 """ if other.__class__ is Vector: return Vector(self.a + other.a, self.b + other.b) elif other.__class__ is int: return Vector(self.
針對 __str__ 方法給出一個比較直觀的例子:
class people: def __init__(self,name,age): self.name=name self.age=age def __str__(self): return '這個人的名字是%s,已經有%d歲了!'%(self.name,self.age) a=people('孫悟空',999) print(a)
輸出:
這個人的名字是孫悟空,已經有999歲了! 如果沒有過載函式的話輸出的就是一串看不懂的字串: <__main__.people object at 0x00000272A730D278>
最新的 Python3.7 中(2018.07.13),對類的建構函式進行了精簡。
3.7 版本:
from dataclasses import dataclass @dataclass class A: x:int y:int def add(self): return self.x + self.y
相當於以前的:
class A: def __init__(self,x,y): self.x = x self.y = y def add(self): return self.x + self.y
Python3 中類的靜態方法、普通方法、類方法
靜態方法: 用 @staticmethod 裝飾的不帶 self 引數的方法叫做靜態方法,類的靜態方法可以沒有引數,可以直接使用類名呼叫。
普通方法: 預設有個self引數,且只能被物件呼叫。
類方法: 預設有個 cls 引數,可以被類和物件呼叫,需要加上 @classmethod 裝飾器。
class Classname: @staticmethod def fun(): print('靜態方法') @classmethod def a(cls): print('類方法') # 普通方法 def b(self): print('普通方法') Classname.fun() Classname.a() C = Classname() C.fun() C.a() C.b()
反向運算子過載:
複合過載運算子:
運算子過載的時候:
#!/usr/bin/python3 class Vector: def __init__(self, a, b): self.a = a self.b = b def __str__(self): return 'Vector (%d, %d)' % (self.a, self.b) def __repr__(self): return 'Vector (%d, %d)' % (self.a, self.b) def __add__(self,other): if other.__class__ is Vector: return Vector(self.a + other.a, self.b + other.b) elif other.__class__ is int: return Vector(self.a+other,self.b) def __radd__(self,other): """反向算術運算子的過載 __add__運算子過載可以保證V+int的情況下不會報錯,但是反過來int+V就會報錯,通過反向運算子過載可以解決此問題 """ if other.__class__ is int or other.__class__ is float: return Vector(self.a+other,self.b) else: raise ValueError("值錯誤") def __iadd__(self,other): """複合賦值算數運算子的過載 主要用於列表,例如L1+=L2,預設情況下呼叫__add__,會生成一個新的列表, 當資料過大的時候會影響效率,而此函式可以過載+=,使L2直接增加到L1後面 """ if other.__class__ is Vector: return Vector(self.a + other.a, self.b + other.b) elif other.__class__ is int: return Vector(self.