问题描述
我有一个看起来像是试图用
a工厂函数生成新样式类的bug。
class Animal(object):pass
级哺乳动物(动物):传递
def newAnimal(bases =(Animal,),dict = {}):
class C (对象):传递
C .__ bases__ = base
dict [''_ count''] = 0
C .__ dict__ = dict
返回C
Canine = newAnimal((哺乳动物))
TypeError:__ bases__赋值:''哺乳动物''deallocator不同来自
''对象''
如果我从C类(对象)语句中删除''对象'',那么我
得到一个不同的,同样令人费解的错误信息:
TypeError:__bases__项必须是类
该函数仅在我有效时才有效从所有基类中删除''对象'。
- 戴夫
************ ************************************** *********** *
* David MacQuigg,博士*电子邮件:dmq at gain.com * *
* IC设计工程师*电话:美国520-721-4583 * * *
*模拟设计方法* * *
* * 9320 East Mikelyn Lane * * *
* VRS Consulting,PC *亚利桑那州图森市85710 *
************************************ ************** *********** *
I have what looks like a bug trying to generate new style classes with
a factory function.
class Animal(object): pass
class Mammal(Animal): pass
def newAnimal(bases=(Animal,), dict={}):
class C(object): pass
C.__bases__ = bases
dict[''_count''] = 0
C.__dict__ = dict
return C
Canine = newAnimal((Mammal,))
TypeError: __bases__ assignment: ''Mammal'' deallocator differs from
''object''
If I remove the ''object'' from the class C(object) statement, then I
get a different, equally puzzling error message:
TypeError: __bases__ items must be classes
The function works only if I remove ''object'' from all base classes.
-- Dave
************************************************** *********** *
* David MacQuigg, PhD * email: dmq at gain.com * *
* IC Design Engineer * phone: USA 520-721-4583 * * *
* Analog Design Methodologies * * *
* * 9320 East Mikelyn Lane * * *
* VRS Consulting, P.C. * Tucson, Arizona 85710 *
************************************************** *********** *
推荐答案
IIRC,你不能分配给新式课程的__bases__。但是你可以使用type()代替你自制的工厂函数。
IIRC, you can''t assign to __bases__ of a new-style class. But you can
use type() instead of your home-made factory function.
IIRC,你不能分配给新式课程的__bases__。但你可以使用type()而不是你自己的工厂功能。
IIRC, you can''t assign to __bases__ of a new-style class. But you can
use type() instead of your home-made factory function.
更好:
见,特别是
classobj!
-
Yermat
Even better:
see http://www.python.org/doc/current/lib/module-new.html and especially
classobj !
--
Yermat
这不是错误。开发人员取消了改变新式班级基础的可能性。可能是因为它无法完成
可靠(我不知道)。但我很高兴我的基础班不能在我脚下改变
;)
如果你愿意的话,你可以创建一个带有新基础的新课程。 />
Michele Simionato
This is not a bug. The developers removed the possibility to change
the bases of a new-style class. Probably because it could not be done
reliably (I don''t know). But I am happy my base classes cannot
change under my feet ;)
You can just create a new class with new bases if you wish.
Michele Simionato
这篇关于新样式中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!