Python中请大家指点
class MyType(type):
def init(self, what, bases=None, dict=None):
print(‘call myType.init()’)
print(“class name:”+what)
print(“class bases:”+str(bases))
print(“class attributions:”+str(dict))
super().init(what, bases, dict)
def new(cls, name, bases, attrs):
print(“call MyType.new()”)
return type.new(cls, name, bases, attrs)
class Foo(object):
metaclass = MyType #语句 1
def init(self, name=None):
self.name = name
print(“Foo.init self=”, self)
print(“Foo self.name=”, self.name)
def new(cls, *args, **kwargs):
print(“Foo.new cls=”, cls)
return(object.new(cls, *args, **kwargs))
def call(self, cls):
print(“Foo.call cls=”, cls)
print(“Foo.call self=”, self)
def func(self):
print(‘function is there’)
class studen(Foo):
print(“studen(Foo)”) #语句 2
def init(self, name=None):
print(“studen.init self=”, self)
class Foo2(object):
“”“docstring for Foo2"”"
def init(self):
print(‘Foo2.init self.class=’, self.class)
print(“Foo2.init type(self)=”, type(self))
if name == ‘main’:
print("---------test1---------") #语句 3
obj2=Foo() #语句 4
print("---------test2---------")
print(“test2 type=”, type(studen()))
print("---------test3---------")
print(“test3 type=”, type(Foo2()))
上面这段代码的输出如下:
studen(Foo)
---------test1---------
Foo.new cls=
Foo.init self= <main.Foo object at 0x01C2A270>
Foo self.name= None
---------test2---------
Foo.new cls=
studen.init self= <main.studen object at 0x01C2A350>
test2 type=
---------test3---------
Foo2 self.class=
Foo2 type(self)=
test3 type=
我的问题如下:
1、为何语句 2 会在语句 3 之前先执行了?是不是因为类定义中的不在__new__()、init()和__call__()的方法之内的语句都会在模块被加载的时候立即执行?
2、在语句 1 中,我已经指定 Foo 类要由 MyType 类来实例化创建,为何执行语句 4 的时候,类 MyType 定义中的__new__()和__init__()方法都没有被首先执行?
恳请指点,感谢!
Python中请大家指点
给楼主贴上我自己 python2 改造后的代码
https://gist.github.com/zhusimaji/31fe157b62703923bf6177f05f0fdbac
依次回答楼主的问题
( 1 )类被倒入加载时,print 没有放在类定义的函数中是会被运行打印
( 2 )语句 4 -> metaclass 是__new__和__init__都是先运行,没有任何问题
我无法理解你的问题
这个可能与 python3 有关,建议楼主看下 python 3 关于 type 的说明
非常感谢啊!
但是关于第二个问题的回答“语句 4 -> metaclass 是__new__和__init__都是先运行,没有任何问题”我还是没明白。
在语句 1 中已经指定 Foo 类要由 MyType 类来实例化创建,所以我觉得执行“ obj2=Foo()”这个语句时,应该就是实例化了一个类 Foo 的对象并赋值给 obj2,但是要根据 MyType 的定义来实例化。
这时应该去执行 MyType 定义中的__new__和__init__,而不是去执行 Foo 定义中的__new__和__init__。我这个理解错在哪里呢?
另外,您修改后的代码在 PYTHON2 下运行,在类被加载时会先去执行 MyType 中的__new__和__init__,然后再执行语句 3。 但是为何在 PYTHON3 下,类被加载时不会执行 MyType 中的语句,而是直接就执行语句 3 呢?
自己再顶一下,上面说的遗留的问题,还请大家不吝赐教,感谢!

