[英]How to call child class methods using parent class object
I am a bit new to Python inheritance. 我对Python继承有点陌生。 I have a code something like this
我有这样的代码
class Parent(object):
'''
Class for Parent
'''
def __init__(self, host, user, password):
self.host = host
self.user = user
self.password = password
self.child = Child()
def method1(self,param1, param2, param3):
self.child.method1(param1, param2, param3)
The below is the child class. 下面是子类。 Both parent and child classes are in the same file
父类和子类都在同一文件中
class Child(Parent):
'''
Class for Child
'''
def __init__(self):
super(Child).__init__()
def method1(self, param1, param2, param3):
// My actual code will be here
Now, I have another python script where I would initialize the parent object and call method1 现在,我还有另一个python脚本,我将在其中初始化父对象并调用method1
client = Parent(host, user, password)
client.method1(param1, param2, param3)
With the above approach, I am expecting that the method1 of Child will get executed. 通过上述方法,我期望Child的method1将被执行。 But it is not.
但事实并非如此。 I am getting the following error
我收到以下错误
TypeError: super() takes at most 1 argument (0 given)
What is the solution for this..? 解决这个问题的办法是什么? Where am I doing wrong..?
我在哪里做错了..?
In Python 2, super
takes two arguments, as the error message explains. 在Python 2中,
super
接受两个参数,如错误消息所述。 You use it like this: 您可以这样使用它:
super(Child, self).__init__()
However, you also have to pass along all the required arguments to __init__
. 但是,您还必须将所有必需的参数传递给
__init__
。 Since your Parent
takes a host
, user
, and password
, you need these to super
the initializer. 由于您的
Parent
使用host
, user
和password
,因此您需要这些来super
初始化程序。 So you probably want something like this in Child
: 所以您可能在
Child
想要这样的东西:
def __init__(self, host, user, password):
super(Child, self).__init__(host, user, password)
Although since Child.__init__
doesn't actually do anything besides try to super
, the simpler thing to do is to just not override __init__
in the first place. 尽管由于
Child.__init__
除了尝试super
之外实际上没有做任何事情,所以更简单的事情是首先不要重写__init__
。
At any rate, once you solve the super
problem, either way, you're just going to have an infinite recursion—the Parent
creates a Child()
, which supers the Parent
constructor and therefore creates another Child()
, and so on until you get an exception. 无论如何,无论哪种方式解决了
super
问题,您都将拥有无限的递归— Parent
创建一个Child()
,它取代了Parent
构造函数,因此创建了另一个Child()
,依此类推,直到你有一个例外。
In fact, your design is very weird. 实际上,您的设计很奇怪。 It's rare that an object needs to hold a reference to (much less create) an instance of one of its subclasses.
很少有一个对象需要持有对其子类之一实例的引用(更不用说创建)了。 Normally, you just want to use inheritance as inheritance.
通常,您只想将继承用作继承。 Something like this:
像这样:
class Parent(object):
'''
Class for Parent
'''
def __init__(self, host, user, password):
self.host = host
self.user = user
self.password = password
self.child = Child()
class Child(Parent):
def method1(self, param1, param2, param3):
# actual code here
Now, if you create a child = Child(host, user, password)
, you can call child.method1(arg1, arg2, arg3)
on it. 现在,如果您创建
child = Child(host, user, password)
,则可以在其上调用child.method1(arg1, arg2, arg3)
。 If you want to provide a default implementation in Parent
, you can. 如果要在
Parent
提供默认实现,则可以。 You can even make it a pre- or post-implementation that Child
and any other children call via super
before or after their own code. 您甚至可以将
Child
和其他任何子代在其自己的代码之前或之后通过super
调用在实现之前或之后。 Which one you actually want is impossible to say with this toy model, but for real classes it's usually pretty obvious. 用这种玩具模型无法说出您真正想要的那个,但是对于真实的类,它通常很明显。
I managed to achieve this with the help of a Singleton class. 我在Singleton课程的帮助下实现了这一目标。 This is what I have done.
这就是我所做的。 This is the parent class.
这是父类。
class Parent(object):
'''
Class for Parent
'''
def __init__(self, host, user, password):
self.host = host
self.user = user
self.password = password
def helper_method1():
// Implementation
Then I've created a singleton class 然后我创建了一个单例课程
class ParentObjMgr(object):
'''
A Singleton that stores the objects of different child classes.
The child class needs to be instantiated here.
This class needs to be instantiated in the caller.
'''
__metaclass__ = Singleton
def __init__(self, host, user, password):
self._host = host
self._user = user
self._password = password
self._child1 = None
self._child2 = None
def get_child1(self):
if not self._child1:
self._child1 = Child1(self._host, self._user, self._password)
return self._child1
def get_child2(self):
if not self._child2:
self._child2 = Child2(self._host, self._user, self._password)
return self._child2
Now I will implement child classes 现在我将实施子班
class Child1(Parent):
// Implementation
class Child2(Parent):
// Implementation
self.helper_method1()
Now in the caller, I would do this 现在在呼叫者中,我将执行此操作
parent_object_mgr=ParentObjMgr(self.host, self.user, self.password)
child1=parent_object_mgr.get_child1()
child2=parent_object_mgr.get_child2()
This is how I have managed to access all the child class objects with the help of a single class object. 这就是我在单个类对象的帮助下设法访问所有子类对象的方式。 Now, I don't have to instantiate the child classes, wherever they are being called and that is what I was looking for.
现在,无论在哪里调用子类,我都不必实例化它们,这就是我想要的。 Please let me know if it is an efficient solution (or) is there anything I need to do to improve it.
请让我知道这是否是一种有效的解决方案(或)我需要做些什么来改进它。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.