草庐IT

python - 使用多重继承时如何使用super来初始化所有父类

HowdoesPython'ssuper()workwithmultipleinheritance?我在看上面的问题/答案,把自己弄糊涂了53classFirst(object):54def__init__(self):55print"first"5657classSecond(First):58def__init__(self):59super(Second,self).__init__()60print"second"6162classThird(First):63def__init__(self):64print"third"6566classFourth(Second,Thir

Python:在初始评估后向类添加父类

一般Python问题我正在导入具有以下类结构的Python库(称为animals.py):classAnimal(object):passclassRat(Animal):passclassBat(Animal):passclassCat(Animal):pass...我想为每个物种类(Rat、Bat、Cat,...);但是,我无法更改正在导入的库的实际来源,因此它必须是运行时更改。以下似乎有效:importanimalsclassPet(object):passforklassin(animals.Rat,animals.Bat,animals.Cat,...):klass.__ba

python - 在 Python 中弃用一个类作为父类

我正在使用Python2.x框架,该框架的最新版本已将一些广泛使用的基类从模块A移至模块B(并且在此过程中这些类已重命名为更清晰的名称)。模块A为新类名定义了向后兼容的标识符。B.py:classBaseClass(object):__metaclass__=framework_meta#handlesregistrationetc.A.py:importBoldbase=B.BaseClass现在为了帮助人们迁移他们的代码,我希望能够在使用框架的代码定义派生自A的类时发出DeprecationWarning(使用warnings.warn).oldbase告诉程序员直接继承自B.Ba

python - 隐式调用父类初始化器

classA(object):def__init__(self,a,b,c):#super(A,self).__init__()super(self.__class__,self).__init__()classB(A):def__init__(self,b,c):printsuper(B,self)printsuper(self.__class__,self)#super(B,self).__init__(1,b,c)super(self.__class__,self).__init__(1,b,c)classC(B):def__init__(self,c):#super(C,sel

python - 字节和字节数组的父类(super class)?

我正在制作一个接受Unicode字符串或字节(或字节数组)对象的函数。我想确保只有那些类型得到通过。我知道我可以通过执行isinstance(x,str)检查某物是否是一个字符串,我知道我可以写isinstance(x,bytes)或isinstance(x,bytearray).是否有更简洁的方法来检查后者,即是否有bytes和bytearray派生的类? 最佳答案 除了object之外没有共同的基类:>>>bytearray.__base__>>>bytes.__base__不要检查类型。让用户传递她想要的任何类型的参数。如果该

python - 在类中调用父类的 __call__ 方法

我想从继承类中调用父类的call方法代码是这样的#!/usr/bin/envpythonclassParent(object):def__call__(self,name):print"helloworld,",nameclassPerson(Parent):def__call__(self,someinfo):super(Parent,self).__call__(someinfo)p=Person()p("info")我明白了,File"./test.py",line12,in__call__super(Parent,self).__call__(someinfo)Attribut

python - 从父类继承的子类中删除特定方法

代码如下,只是基本结构:classFooType(type):def__new__(cls,name,bases,classdict):instance=type.__new__(cls,name,bases,classdict)#WhatcanIdohere?returninstanceclassFooBase(object,metaclass=FooType):def__init__(self):passclassFoo(FooBase):def__init__(self,name):self.name=namedefmethod1(self):passdefmethod2(sel

python - 如何在 Python 中访问父类(super class)的元属性?

我有一些这样的代码Django-Tastypie:classSpecializedResource(ModelResource):classMeta:authentication=MyCustomAuthentication()classTestResource(SpecializedResource):classMeta:#thefollowingstyleworks:authentication=SpecializedResource.authentication#butthefollowingstyledoesnot:super(TestResource,meta).authen

python - 我如何正确地继承具有 __new__ 方法的父类(super class)?

假设我们有一个类'Parent',由于某种原因定义了__new__和一个继承自它的类'Child'。(在我的例子中,我试图从我无法修改的第3方类继承)classParent:def__new__(cls,arg):#...somethingimportantisdoneherewitharg我的尝试是:classChild(Parent):def__init__(self,myArg,argForSuperclass):Parent.__new__(argForSuperclass)self.field=myArg但是同时p=Parent("argForSuperclass")按预期工

python - 从子类实例访问父类实例属性?

在此代码示例中如何从“child”访问“myvar”:classParent():def__init__(self):self.myvar=1classChild(Parent):def__init__(self):Parent.__init__(self)#thiswon'tworkParent.myvarchild=Child() 最佳答案 父类是一个类——蓝图不是它的一个实例,在OOPS中访问一个对象的属性,它需要相同的实例,这里self/child是实例,而Parent/Child是类......看下面的答案,或许可以解开你