新样式中的错误 [英] Bug in New Style Classes
问题描述
我有一个看起来像是试图用
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 *
************************************************** *********** *
推荐答案
David MacQuigg写道:
David MacQuigg wrote:
我看起来像是一个试图用
工厂函数生成新样式类的bug。
I have what looks like a bug trying to generate new style classes with
a 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.
Leif K-Brooks写道:
Leif K-Brooks wrote:
David MacQuigg写道:
David MacQuigg wrote:
我看起来像是一个试图用
工厂函数生成新样式类的bug。
I have what looks like a bug trying to generate new style classes with
a 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.
更好:
见 http://www.python.org/doc/current/ lib / module-new.html ,特别是
classobj!
-
Yermat
Even better:
see http://www.python.org/doc/current/lib/module-new.html and especially
classobj !
--
Yermat
David MacQuigg< dm*@gain.com>在消息新闻中写道:< rm ******************************** @ 4ax.com>。 ..
David MacQuigg <dm*@gain.com> wrote in message news:<rm********************************@4ax.com>. ..
我有一个看起来像是试图用
工厂函数生成新样式类的bug。
类Animal(对象):传递
类哺乳动物(动物):传递
def newAnimal(bases =(Animal,),dict = {}):
C类(对象):传递
C .__ bases__ = base
dict [''_ count''] = 0
C .__ dict__ = dict
返回C
Canine = newAnimal((哺乳动物))
TypeError:__bases__赋值:''哺乳动物''deallocator与
''对象'不同
如果我从类C(对象)语句中删除''对象'',然后我得到一个不同的,同样令人费解的错误信息:
TypeError:__bases__项必须是类
该函数只有在我删除''object'时才有效来自所有基类。
- 戴夫
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
这不是错误。开发人员取消了改变新式班级基础的可能性。可能是因为它无法完成
可靠(我不知道)。但我很高兴我的基础班不能在我脚下改变
;)
如果你愿意的话,你可以创建一个带有新基础的新课程。 />
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
这篇关于新样式中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!