我经常在重写子类中的方法时这样做:defmethod_x(self):x=super(type(self),self).method_x()[Someextracode]returnx我的问题是:super(type(self),self)有捷径吗? 最佳答案 不要那样做:如果super可以只使用type(self)作为它的第一个参数,那么它就不会被写成在第一名。您必须在此处传递实际类,而不是表达式,如果类已被子类化,表达式可能会发生变化。super的第一个参数需要是包含当前方法定义的类,因为您要告诉super在碱基列表中的何处开始
我的changepassword表单有一些问题,它继续给我同样的错误:super(type,obj):objmustbeaninstanceorsubtypeoftype这是我的表格:classPasswordChangeForm(forms.Form):current_password=forms.CharField(label=u'CurrentPassword',widget=forms.PasswordInput(render_value=False))new_password=forms.CharField(label=u'NewPassword',widget=forms.
在下面的示例中,B.Go()方法的最后两行都调用了classA中的Go()方法>。它们在功能上是否相同?使用super()的唯一好处是我不必知道继承的类名吗?classA(object):defGo(self):print"CallingA.Go()"classB(A):defGo(self):super(B,self).Go()A.Go(self)inst=B()inst.Go() 最佳答案 不,super()做一些直接调用A.Go不能的事情。super(B,self).Go()调用方法解析顺序中的下一个方法。如果B根本没有实现该
我知道__new__是一个静态方法,可以从中调用super()创建一个新对象,如下所示:>>>classA:...def__new__(cls):...print('__new__called')...returnsuper().__new__(cls)...>>>a=A()__new__called为什么super调用不能与其他静态方法一起使用?为什么以下会失败?>>>classB:...@staticmethod...deffuncB():...print('funcBcalled')...>>>classC(B):...@staticmethod...deffuncC():...
classWorks(type):def__new__(cls,*args,**kwargs):print([cls,args])#outputs[,()]returnsuper().__new__(cls,args)classDoesNotWork(type):def__new__(*args,**kwargs):print([args[0],args[:0]])#outputs[,()]returnsuper().__new__(args[0],args[:0])Works()#isfineDoesNotWork()#gets"RuntimeError:super():noargu
我想写一个文件。根据文件的名称,这可能会或可能不会被gzip模块压缩。这是我的代码:importgzipfilename='output.gz'opener=gzip.openiffilename.endswith('.gz')elseopenwithopener(filename,'wb')asfd:print('blahblahblah'.encode(),file=fd)我正在以二进制模式打开可写文件并对要写入的字符串进行编码。但是我收到以下错误:File"/usr/lib/python3.5/gzip.py",line258,inwritedata=memoryview(dat
最近在项目调试中,获取手机的IMSI,IMEI等信息,发现在Android10以下系统的设备上正常,但是在Android10以上系统的设备上报错:Theuser10116doesnotmeettherequirementstoaccessdeviceidentifiersprivatestaticStringgetSimImsi(Contextcontext){StringsimImsi=null;try{TelephonyManagertm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);simIms
我正在运行一个已编译的Python脚本,该脚本使用Selenium启动一个ChromeWebdriversession,该session访问一个站点并执行一些任务。该脚本的行为与我预期的一样,除了它在我第一次启动webdriver时向控制台打印一条“错误”消息。错误如下:[2460:7268:1121/133303:ERROR:base_feature_provider.cc(122)]manifestTypes:Allowingweb_pagecontextsrequiressupplyingavalueformatches.谁知道这是什么意思?就像我上面所说的,脚本的行为似乎符合我
根据http://docs.python.org/2/library/functions.html#super,Ifthesecondargumentisomitted,thesuperobjectreturnedisunbound.哪个是super(类型)。我想知道什么是无界的,什么时候是有界的。 最佳答案 您问题的其他答案(answer、answer)已经解释了绑定(bind)/未绑定(bind)这两个词的含义。Somyfocusistoexplainonlytheuseofanunboundproxyobjectreturne
我使用setuptools'tests_require'来指定测试我的包所需的依赖项。tests_require-http://pythonhosted.org/distribute/setuptools.html#new-and-changed-setup-keywords我已经开始使用wheelpackaginghttp://wheel.readthedocs.org/en/latest/并为我当前的包及其所有依赖项构建一个wheels目录。pipwheel--wheel-dir=/tmp/wheelhouse.不过,我还想为任何包tests_require中列出的所有包构建轮子。