[英]Python: Class Inheritance and Incoming Arguments
ClassA to inherits from Base class which inherits from built-in dict class. ClassA继承自继承自内置dict类的Base类。 'name' and 'id' are Base class attributes. 'name'和'id'是基类属性。 'win' and 'mac' are attributes ClassA attributes. 'win'和'mac'是属性ClassA属性。 How should I put a logic in this code so classA instance could be declared as easy as: 我应该如何在此代码中放置逻辑,以便可以将classA实例声明为:
myInstance=ClassA(myDictArg)
=============================== ===============================
class Base(dict):
"""Base is the base class from which all other classes derrive.
Base class inherits from build-in dict type.
"""
id = 'id'
name = 'name'
def __init__(self, arg=None):
"""Initialise Base Class"""
dict.__init__(self)
self[Base.id] = -1
self[Base.name] = None
if 'id' in arg.keys() and arg['id']: self['id']=arg['id']
if 'name' in arg.keys() and arg['name']: self['name']=arg['name']
class ClassA(Base):
"""ClassA is a class inherited from a Base class."""
def __init__(self, arg=None):
if arg==None: raise Exception('arg==None')
Base.__init__(self)
self.arg = arg
# set a generic to ClassA Attrs
self['win']=None
self['mac']=None
myDictArg= {'id':1, 'name':'MyName', 'win':'c:/windows', 'mac': '/Volumes/mac/'}
myInstance=ClassA(myDictArg)
print myInstance
This class structure has the advantage that it keeps the signature of dict
which is pretty flexible and sets default values only if they aren't provided (which I think was the original goal). 这个类结构的优点是它保留了dict
的签名,它非常灵活,只有在没有提供时才设置默认值(我认为这是最初的目标)。 It also (due to judicious use of super
) is well set up to support cooperative multiple inheritance (Horray!). 它(由于明智地使用super
)也很好地设置了支持合作多重继承(Horray!)。
class Base(dict):
def __init__(self, *args, **kwargs):
super(Base, self).__init__(*args, **kwargs)
self.setdefault('id', -1)
self.setdefault('name', None)
class ClassA(Base):
"""ClassA is a class inherited from a Base class."""
def __init__(self, *args, **kwargs):
if not (args or kwargs):
raise Exception('you need to give me *something*!')
super(ClassA, self).__init__(*args, **kwargs)
self.setdefault('win', None)
self.setdefault('mac', None)
What you've written looks like it should work.. not that I've ran it myself. 你写的东西看起来应该有用......不是我自己运行它。 So I am making an assumption that you are looking for the bug in this situation... one possibility for a problem is the fact that you are replacing the arg variable after 'id' and 'name' have been set, effectively erasing them.. I think a better idea would be to merge the args. 因此,我假设您正在寻找这种情况下的错误...问题的一个可能性是您在设置'id'和'name'之后替换arg变量,从而有效地擦除它们。我认为更好的想法是合并args。 Although the following code may not be the most pythonic.. It might look something like this. 虽然以下代码可能不是最pythonic ..它可能看起来像这样。
for key in arg.keys()
self.arg[key] = arg[key]
another problem is that you aren't even passing in your args object into the base class's constructor. 另一个问题是你甚至没有将你的args对象传递给基类的构造函数。 I suggest you change that to 我建议你把它换成
Base.__init__(self, args)
Otherwise, arg in the Base class will revert to the default; 否则,Base类中的arg将恢复为默认值; None. 没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.