class Person(object):
#this __new__ function will be called how to built this object
def __new__(cls,name,age):
print("this is new")
print("memonery is %d"%id(cls))
return object.__new__(cls)
#when the __new__ function has been called successfully.this function will be called to initialization this object
def __init__(self,name,age):
print("this is init")
self.name = name
self.age = age
#when the object will be destroy,this function will be called
def __del__(self):
print("deinit")
#when you use `print(object)` to print object
#object will open this return string
def __str__(self):
return "%s's age is %d"%(self.name,self.age)
person = Person("xiaoming",18)
print(person)
Python中的一些特殊的方法
最新推荐文章于 2022-04-15 20:52:35 发布