草庐IT

Super-Resolution

全部标签

python - '__init__' 对象的描述符 'super' 需要参数

我正在尝试用Python制作一个面向对象的基于文本的游戏,并尝试实现我的第一个属性和装饰器。使用本书第5章'Python3ObjectOrientedProgramming',我尝试使用所讨论的示例和概念来获取以下代码以在实例化时设置游戏对象的“current_room”属性:classRoom(object):'''Anareaofthegame'smap.'''def__init__(self):print("AccessingtheRoom__init__method.")classFirstRoom(Room):'''Justsomeroom.'''def__init__(se

python - 相当于 functools.singledispatch 的 super()

functools.singledispatch有助于定义单次分派(dispatch)泛型方法。同时,有super()用于调用方法或访问父类(superclass)的属性。是否有类似super()的东西可以与singledispatch一起使用?我尝试了以下方法,但是super(Derived,value)的结果不是Base的实例,所以它没有像我预期的那样工作:fromfunctoolsimportsingledispatch@singledispatchdefhello(value):return['default']@hello.register(Base)defhello_bas

python - 为什么类方法的 super 需要第二个参数?

这按预期工作:>>>classFoo(object):...@classmethod...defhello(cls):...print'hello,foo'...>>>classBar(Foo):...@classmethod...defhello(cls):...print'hello,bar'...super(Bar,cls).hello()...>>>b=Bar()>>>b.hello()hello,barhello,foo我也可以显式调用基类:>>>classBar(Foo):...@classmethod...defhello(cls):...print'hello,bar'

Python类-- super 变量

由于某种原因,下面的代码给我一个错误,有人能告诉我问题出在哪里吗..基本上,我创建了2个类Point和Circle..Thecircle试图继承Point类。Code:classPoint():x=0.0y=0.0def__init__(self,x,y):self.x=xself.y=yprint("Pointconstructor")defToString(self):return"{X:"+str(self.x)+",Y:"+str(self.y)+"}"classCircle(Point):radius=0.0def__init__(self,x,y,radius):super

python3 - super() 在多重继承上的行为

我知道这里已经讨论了super()和多重继承。但是我没有找到解决方案,关于我在python3中的具体问题。假设我们有:#!/usr/bin/envpython3classA(object):def__init__(self):super().__init__()deffoo(self):print("The")classB(object):def__init__(self):super().__init__()deffoo(self):print("world")classC(B):def__init__(self):super().__init__()deffoo(self):sup

python - super 迷惑的python多重继承super()

我在玩弄python中的多重继承,遇到了一个我无法理解它是如何发生的情况。这是继承布局:AF/\|BC|\|/\|/D大家耳熟能详的ABCD钻石。再加上一个额外的“F”类,我把它扔进去是为了好玩。代码如下:classA(object):deffoo(self,call_from):print"foofromA,callfrom%s"%call_fromsuper(A,self).foo("A")classB(A):deffoo(self,call_from):print"foofromB,callfrom%s"%call_fromsuper(B,self).foo("B")classC

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

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

python - super() 与非直接父级一起使用

这是对super()的合法使用吗?classA(object):defmethod(self,arg):passclassB(A):defmethod(self,arg):super(B,self).method(arg)classC(B):defmethod(self,arg):super(B,self).method(arg)谢谢。 最佳答案 它会起作用,但它可能会使任何试图阅读您的代码的人(包括您,除非您特别记得)感到困惑。不要忘记,如果你想从一个特定的父类调用一个方法,你可以这样做:A.method(self,arg)

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 - 如何模拟父类(super class)的 __init__ 创建一个包含用于单元测试的模拟对象的属性?

我正在尝试为类的__init__编写单元测试:def__init__(self,buildNum,configFile="configfile.txt"):super(DevBuild,self).__init__(buildNum,configFile)ifconfigFile=="configfile.txt":self.config.MakeDevBuild()config属性由super的__init__设置。我正在使用mock,并且我希望config属性是一个模拟对象。但是,我一直无法弄清楚如何真正实现这一目标。这是我能想到的最好的测试:deftest_init(self):