class MyClass(object):
def __new__(cls, *args, **kargs) :
print('__new__ method is called and id is {0}'.format(id(cls)))
obj = super().__new__(cls)
print('id of object is {0}'.format(id(obj)))
return obj;
def __init__(self, name, age) :
super().__init__()
print('__init__ method is called and id is {0}'.format(id(self)))
self.name = name
self.age = age
def say_hello(self) :
print('hello {0}, you are {1}'.format(self.name, self.age))
print('id of class object is {0}'.format(id(object)))
print('id of class MyClass is {0}'.format(id(MyClass)))
my_class = MyClass('Smith', 30)
my_class.say_hello();
Python———__init__()和__new__()调用与重写
最新推荐文章于 2024-07-27 12:20:46 发布