class Parent1(object):
def foo(self):
print "P1 foo"
def bar(self):
print "P1 bar"
class Parent2(object):
def foo(self):
print "P2 foo"
def bar(self):
print "P2 bar"
class Child(Parent1, Parent2):
def foo(self):
super(Parent1, self).foo()
def bar(self):
super(Parent2, self).bar()
c = Child()
c.foo()
c.bar()
Run Code Online (Sandbox Code Playgroud)
目的是从 Parent1 继承 foo(),从 Parent2 继承 bar()。但是 c.foo() 对parent2 和 c.bar() 结果是错误的。请指出问题并提供解决方案。
您好,社区,我正在学习OOPS概念与python作为我的课程的一部分.我在python中遇到多重继承问题.以下是我的代码:
#!/usr/bin/env python
class Base1(object):
def __init__(self):
self.base1var = "this is base1"
class Base2(object):
def __init__(self):
self.base2var = "this is base2"
class MainClass(Base1, Base2):
def __init__(self):
super(MainClass, self).__init__()
if __name__ == "__main__":
a = MainClass()
print a.base1var
print a.base2var
Run Code Online (Sandbox Code Playgroud)
并且在运行时,我收到以下错误
print a.base2var
AttributeError: 'MainClass' object has no attribute 'base2var'
Run Code Online (Sandbox Code Playgroud)
如果我交换继承的类的顺序,则错误中的变量名会相应地更改.
super()当我想调用两个继承类的构造函数时,我是否使用了错误?
如何才能正确地从多个基类继承并使用主类中的变量和方法而不会出现此错误?
谢谢.
假设我定义了四个类如下:
(该代码已经在 Python 3.6.5 上进行了测试。但是,我希望它也可以在 Python 2.7.x 上使用from __future__ import print_function)
In [1]: class A(object):
...: pass
...:
...: class B(object):
...: def __init__(self, value):
...: print('B(value=%s)' % value)
...:
...: class C(A):
...: def __init__(self, value):
...: print('C(value=%s)' % value)
...: super(C, self).__init__(value)
...:
...: class D(A, B):
...: def __init__(self, value):
...: print('D(value=%s)' % value)
...: super(D, self).__init__(value)
...:
In [2]: C.mro()
Out[2]: [__main__.C, __main__.A, object]
In [3]: D.mro()
Out[3]: …Run Code Online (Sandbox Code Playgroud) 笔记:
问题:为什么不在下面给定的代码中在A 构造函数之后打印C 构造函数?:
class A(object):
def __init__(self):
print("A Constructor")
class B(A):
def __init__(self):
print("B Constructor")
super(B, self).__init__()
class C():
def __init__(self):
print("C Constructor")
super().__init__()
def method(self):
print("C method")
class D(B, C):
def __init__(self):
print("D Constructor")
super(D, self).__init__()
super().method()
d = D()
print(D.__mro__)
Run Code Online (Sandbox Code Playgroud)
更准确地说。我目前正在从Corey Schafers教程系列学习 Flask,我正处于登录身份验证阶段,一切似乎都很好,但在尝试登录后出现此错误:
AttributeError
AttributeError: 'User' object has no attribute 'is_active'
Traceback (most recent call last)
File "C:\Users\panda\Desktop\HDD\Programs\Python3\Lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\panda\Desktop\HDD\Programs\Python3\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\panda\Desktop\HDD\Programs\Python3\Lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\panda\Desktop\HDD\Programs\Python3\Lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\panda\Desktop\HDD\Programs\Python3\Lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\panda\Desktop\HDD\Programs\Python3\Lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\panda\Desktop\HDD\Programs\Python3\Lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb) …Run Code Online (Sandbox Code Playgroud) 可能重复:
了解Python super()
类的B子类A,所以在B中__init__我们应该__init__像这样调用A :
class B(A):
def __init__(self):
A.__init__(self)
Run Code Online (Sandbox Code Playgroud)
但是super(),我看到这样的事情:
class B(A):
def __init__(self):
super(B, self).__init__() #or super().__init__()
Run Code Online (Sandbox Code Playgroud)
我的问题是:
为什么不super(B, self).__init__(self)呢?仅仅因为返回代理对象是绑定的?
如果我在super中省略第二个参数并且返回代理对象是未绑定的,那么我应该写super(B).__init__(self)吗?
就像下面的代码一样,super(MdiChild, self).__init__()我们在超级函数中传递了mdichild。请向我解释为什么以及为什么.__init__()使用它。
class MdiChild(QtGui.QTextEdit):
sequenceNumber = 1
def __init__(self):
super(MdiChild, self).__init__()
Run Code Online (Sandbox Code Playgroud) 我有一些我正在尝试分析的现有代码。我可以通过@profile使用kernprof添加装饰器来成功地排列配置文件类方法。
有没有一种通用的方法来分析类实例化?我有几个类具有非常复杂的继承结构。当我尝试分析它们的 init 函数时,我得到如下信息:
Line # Hits Time Per Hit % Time Line Contents
==============================================================
179 def __init__(self, data):
180 1 8910739.0 8910739.0 100.0 super().__init__(data)
181 1 10.0 10.0 0.0 self.mortgage_rate = 5.2 # rate in percentage
Run Code Online (Sandbox Code Playgroud)
这有点没用,因为我不知道__init__正在调用什么实际的父函数(这个类有 2 个父级,每个父级都有一个或多个父级)。
有什么办法可以做得更好吗?例如,有没有办法自动潜入每条线,并分析它调用的线(深度有限)?
我有一个ExampleSim从基类继承的类Physics:
class Physics(object):
arg1 = 'arg1'
def physics_method:
print 'physics_method'
class ExampleSim(Physics):
print 'example physics sim'
Run Code Online (Sandbox Code Playgroud)
想象一下这些包含大量代码的类。现在,我Physics通过定义一个新类PhysicsMod并继承自Physics:
class PhysicsMod(Physics):
arg1 = 'modified arg1'
Run Code Online (Sandbox Code Playgroud)
但ExampleSim我为此又创建了一个新类ExampleSimMod并继承自ExampleSim:
class ExampleSimMod(ExampleSim):
print 'modified example sim'
Run Code Online (Sandbox Code Playgroud)
我的问题是,ExampleSimMod继承ExampleSim从继承Physics在那里我想有它继承PhysicsMod来代替。有没有办法做到这一点?也许通过super()或通过多重继承?
class ExampleSimMod(ExampleSim, PhysicsMod):
print 'modified example sim'
Run Code Online (Sandbox Code Playgroud) 我有一个类D,这个类继承的类B和C,这既继承了A,但是当我创建类的实例D发生此错误:
johni@johni-pc:~/Draft$ python3.4 main.py
Traceback (most recent call last):
File "main.py", line 35, in <module>
d = D()
File "main.py", line 31, in __init__
super().__init__()
File "main.py", line 19, in __init__
super().__init__("pg")
TypeError: __init__() takes 1 positional argument but 2 were given
Run Code Online (Sandbox Code Playgroud)
我不明白这一点.
这些类B和C被初始化参数driver类A.我的档案main.py:
class A:
def __init__(self, driver):
self.driver = driver
@property
def driver(self):
return self.__driver
@driver.setter
def driver(self, …Run Code Online (Sandbox Code Playgroud) 我如何使用__str__其他类的多个?例如:
class A:
def __str__(self):
return "this"
class B:
def __str__(self):
return "that"
class C(A,B):
def __str__(self):
return super(C, self).__str__() + " those"
# return something(A) + " " something(B) + " those"
cc = C()
print(cc)
Run Code Online (Sandbox Code Playgroud)
输出:这些
我希望输出为:这就是那些
这篇文章几乎是一个解决方案(有super())
python ×11
inheritance ×6
class ×4
oop ×3
python-2.7 ×2
constructor ×1
flask ×1
flask-login ×1
pyqt4 ×1
python-3.x ×1
string ×1
super ×1