-
__init__() method: the initialization method of the class, called when the class object is initialized, and then called when needed
Note: The return value of this method must be
class Rectangle(): def __init__(self,width,height): self.width =width self.height=height
class CapStr(str): def __new__(cls,string): string=str.upper(string) return str.__new__(cls,string) a=CapStr('I will be transfer') print(a) #结果 I WILL BE TRANSFER
The __del__() method is called when the garbage collection mechanism reclaims the object . This method is not necessarily called when the object is deleted . This method is called only when the content in the object address is deleted.
def __del__ (self): print ( ' __del__ method was called ' ) b,c=a,a c = a print ( ' del a ' ) del a print ( ' del b ' ) del b print ( ' del c ' ) del c # __del__ method is called