类的自带方法执行顺序

# -- encoding:utf-8--
'''
类的自带方法
__call__   类可以带参数
__new__    实例化之前执行,在构造方法之前
__init__   构造方法
__del__    析构方法
__str__    返回实例的描述
__class__  返回所属类名
__module__ 返回当前实例所属模块
__metaclass__ 用来定义一个类以何种形式来创建 ''' class MyType(type): def __init__(self, what, bases=None, dict=None): print("--MyType init--") super(MyType, self).__init__(what, bases, dict) def __call__(self, *args, **kwargs): print("--MyType call--") obj = self.__new__(self, *args, **kwargs) self.__init__(obj, *args, **kwargs) class People(object): skin = "yellow" __metaclass__ = MyType def __init__(self, name, age): print("--People init--") self.name = name self.age = age self.data = {} # People("小白", "26")("a", "b", "c") xyz对应值为abc # def __call__(self, x, y, z): # print("__call__方法" , x, y, z) def __new__(cls, *args, **kwargs): print("--People new--") return object.__new__(cls) #更改对实例的描述,不打印内存地址 def __str__(self): # print("__str__方法") return "人类" def say(self): print("你好啊!") p = People("小白", "26") print(type(People)) #People类是通过type类创建的 print(type(p)) #父类是People print(p) #打印"人类"

运行结果如下:  

posted @ 2018-04-27 21:06  与君同悦  阅读(158)  评论(0)    收藏  举报