python2.7中,我有一个parent类和test类,parent类的__init() 函数中有一个 name属性,现在使用 type() 关键字从 parent、test…
Jump to turn (6)
- 1python2.7中,我有一个parent类和test类,parent类的init() 函数中有一个 name属性,现在使用 type() 关键字从 paren
- 2还有其他写法吗,不想写一个辅助函数 studentinit
- 3你现在这种写法会报错, init() should return None,not ''tuple
- 4首先,第一种方式你说的双分号 ; 在哪里呢我没看到,然后第二种方式具名函数中的Parent和Test没有定义怎么解决
- 5使用具名函数报错了,unbound method init() must be called with Test instance as first argum
- 6还是报错 super(type, obj): obj must be an instance or subtype of type
- User
python2.7中,我有一个parent类和test类,parent类的__init() 函数中有一个 name属性,现在使用 type() 关键字从 parent、test 派生出 student 类, parent、test初始化函数的参数都是 *args,怎么做才能让student 实例化后也有两个父类对象的属性
- Assistant
要在一个由
type关键字动态生成的子类中包含两个父类的属性,你可以按照以下步骤进行:- 首先,通过继承关键字
super()在父类和子类之间共享属性; - 在通过
type()构建子类时,为其添加一个新的__init__方法; - 在这个新的
__init__方法中,调用两个父类的__init__方法,将它们的属性添加到子类。
下面是一段示例代码,演示如何实现这个过程:
python - 首先,通过继承关键字