【Python中super关键字用法实例分析】教程文章相关的互联网学习教程文章

python – 使用super的构造函数的子类构造函数【代码】

最初定义了这个class Mammal(object):def __init__(self, name):self.name = namedef get_name(self):return self.namedef say(self):return("What does the " + self.name + " says")但现在我们要创建Mammals的子类,其构造函数将使用正确的名称调用Mammal的构造函数.class Dog(Mammal):def __init__(self):Dog.self这是我的代码.它说类型对象’狗’没有属性’自己’有什么问题? 什么时候打印(狗().get_name())我应该得到狗.解决方...

Python:当需要参数时,使用super()调用基类的__init __()方法【代码】

参见英文答案 > Python super() raises TypeError 4个我试图在超类中调用__init __()方法,其中所述方法接受参数,但它似乎不起作用.请参阅以下代码:>>> class A:def __init__(self, param1, param2):self._var1 = param1self._var2 = param2>>> class B(A):def __init__(self, param1, param2, param3):super(B, self).__init__(param1, param2)self._var3 = param3>>> a = A("Hi", "Bob") >>>...

在一般情况下,Python的super()实际上是如何工作的?【代码】

super()上有很多很棒的资源,包括this个很棒的博客帖子,以及Stack Overflow上的很多问题.但是我觉得他们都没有解释它在最常见的情况下是如何工作的(使用任意继承图),以及在幕后发生的事情. 考虑这个钻石继承的基本例子:class A(object):def foo(self):print 'A foo'class B(A):def foo(self):print 'B foo before'super(B, self).foo()print 'B foo after'class C(A):def foo(self):print 'C foo before'super(C, self).foo()print...

python – 在类中使用super()【代码】

我试图以这种方式使用super()作为一个简单的类层次结构:class Employee(object):def __init__(self, name):self.name = nameclass FullTime(Employee):def __init__(self, name, satOff, freeDays=[], alDays=[], programDays=[]):global satsOffDictglobal dayDictsuper(Employee, self).__init__(name)但是,我收到此错误:TypeError: object.__init__() takes no parameters我已经读过你需要创建父对象类型对象(新样式类)才能让...

Python3 super()和泛型类【代码】

我相信一个测试用例胜过千言万语:#!/usr/bin/env python3def generate_a(key):class A(object):def method(self):return {'key': key,}return ABaseForB = generate_a(1337)class B(BaseForB):def method(self):dict = super(BaseForB, self).method()dict.update({'other_key': 0,})return dictEXPECTED = {'other_key': 0, 'key': 1337,} RESULT = B().method()if EXPECTED == RESULT:print("Ok") else:print("EXPECTED: ", EXP...

python3 推荐使用super调用base类方法【代码】

from:https://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p07_calling_method_on_parent_class.html 8.7 调用父类方法问题 你想在子类中调用父类的某个已经被覆盖的方法。解决方案 为了调用父类(超类)的一个方法,可以使用 super() 函数,比如:class A:def spam(self):print(A.spam)class B(A):def spam(self):print(B.spam)super().spam() # Call parent spam() super() 函数的一个常见用法是在 __init__() 方法中确保...

python – OrderedDict为什么不使用super?【代码】

我们可以通过使用多重继承来轻松创建OrderedCounter:>>> from collections import Counter, OrderedDict >>> class OrderedCounter(Counter, OrderedDict): ... pass ... >>> OrderedCounter('Mississippi').items() [('M', 1), ('i', 4), ('s', 4), ('p', 2)]如果我错了,请纠正我,但这至关重要依赖于Counter uses super的事实:class Counter(dict):def __init__(*args, **kwds):...super(Counter, self).__init__()...也就...

python super() – __new__ 上下文中的两个参数版本【代码】

参见英文答案 > what does `super()` in `__new__` 3个我已多次阅读the documentation for super()但仍然没有得到两个参数版本返回的内容.Return a proxy object that delegates method calls to a parent orsibling class of type. This is useful for accessing inherited methodsthat have been overridden in a class.>什么是代理对象?>父母还是兄弟姐妹?If the second argument is an o...

为什么以及如何使用Python的super(type1,type2)?【代码】

超有2个参数,super(type, obj_of_type-or-subclass_of_type)我理解如何以及为什么使用super作为obj_of_type的第二个arg.但我不明白第二个arg是子类的问题. 任何人都可以说明原因和方式?解决方法:如果要调用实例方法,则传递对象.如果要调用类方法,则传递类. 对类方法使用super()的经典示例是使用工厂方法,您希望调用所有超类工厂方法.class Base(object):@classmethoddef make(cls, *args, **kwargs):print("Base.make(%s, %s) sta...

python – 为什么super()的隐含特性是一个理想的特性?【代码】

这是一个参考very popular super( ) question的附加问题.问题基本上是“我为什么要使用super()”,答案是“因为你不必引用父类,这可能很好. “ 我的问题是……世界上为什么这是一件好事?无论发生什么事“明确比隐含更好?”假设我正在阅读其他开发人员的代码,我看到:class Foo(Bar, Baz):def my_method(self):super(Foo, self).who_knows( ) #nobody knows!我现在必须深入研究Bar和Baz以及它们所有父类的文档,直到树中找出who_kno...

在子类化不是从`object`派生的python类时使用’super'(旧式?)【代码】

我正在使用std库模块optparser中的子类化OptionParser. (Python 2.5.2)当我尝试它时,我得到了异常:TypeError: super() argument 1 must be type, not classobj查看OptionParser,它不是从对象派生的.所以我添加了对象作为父类,(如下所示)和super工作正常.from optparse import OptionParser, Option class MyOptionParser(OptionParser, object):"""Class to change """def __init__(self,usage=None,option_list=None,option_clas...

python:super() – 类似于在指定类中启动MRO搜索的代理对象【代码】

根据文档,super(cls,obj)返回a proxy object that delegates method calls to a parent or siblingclass of type cls我理解为什么super()提供了这个功能,但我需要稍微不同的东西:我需要创建一个代理对象,将方法调用(和属性查找)委托给类cls本身;并且在super中,如果cls没有实现方法/属性,我的代理应继续查看MRO顺序(新的而不是原始类).有没有我能写的功能可以达到这个目的? 例:class X:def act():#...class Y:def act():#...clas...

python小知识---super是干嘛的

super用于继承父类的方法、属性 super是新式类中才有的,所以Python2中使用时,要在类名的参数中写object。Python3默认是新式类,直接可用。 super能提供代码的复用性、可维护行。 代码举例: class Dog():def dog_talk():print("wang wang wang!") class LaDuo(Dog):def laduo_talk():super().dog_talk() da_erduo = LaDuo() da_erduo.dog_talk()

在Python中使用super执行父级函数【代码】

我在Python中使用super函数时遇到了一些问题.假设我有这两个类:class A(object):x=5def p(self):print 'A'def f(self):self.p()self.x+=1class B(A):def p(self):print 'B'def f(self):super(B, self).f()self.x*=2b = B() b.f()然后b.x将等于12,但函数将输出’B’,而不是’A’.我需要的是执行A.p而不是B.p,我该如何实现? 谢谢你的时间 :) 编辑:好的,我认为你错过了一些关于我实际情况的细节,因为我的例子不好.让我们来看看真正...

实例 - 相关标签