在python继承层次结构中管理arg和kwarg

在python继承层次结构中管理arg和kwarg,python,function,inheritance,arguments,Python,Function,Inheritance,Arguments,有没有什么“好”的方法可以像我在这段代码中尝试的那样在继承层次结构中同时管理arg和kwarg。我的意思是不需要用kwargs或类似的东西来获得一个指定键的值 它应该显示1234: class Parent(object): def __init__(self, motherArg1, motherArg2=100): self.motherArg1 = motherArg1 self.motherArg2 = motherArg2 def pri

有没有什么“好”的方法可以像我在这段代码中尝试的那样在继承层次结构中同时管理arg和kwarg。我的意思是不需要用kwargs或类似的东西来获得一个指定键的值

它应该显示
1234

class Parent(object):
    def __init__(self, motherArg1, motherArg2=100):
        self.motherArg1 = motherArg1
        self.motherArg2 = motherArg2
    def printParent(self):
        print self.motherArg1
        print self.motherArg2

class Child(Parent):
    def __init__(self, childArg1, *args, childArg2=100, **kwargs): # Doesn't work here
        super(Child, self).__init__(*args, **kwargs)
        self.childArg1 = childArg1
        self.childArg2 = childArg2
    def printChild(self):
        print self.childArg1
        print self.childArg2

child = Child(1, 3, childArg2=2, motherArg2=4)
child.printChild()
child.printParent()
语法不好:在*args之后应为“;”

def\uuuuu init\uuuuu(self,childArg1,childArg2=100,*args,**kwargs)
是正确的语法,但不起作用

  • 当我尝试这种语法并且
    child=child(1,childArg2=2,3,motherArg2=4)
    时,我得到语法错误:关键字arg之后是非关键字arg
  • 当我尝试
    child=child(1,3,childArg2=2,motherArg2=4)
    时,我得到TypeError:\uuu init\uuuu()为关键字参数'childArg2'得到了多个值。

  • 当您将父级的
    printChild
    重命名为
    printpparent
    (并修复
    print
    s)时,它已经在python 3中工作,如建议的:

    1
    2
    3
    4
    
    但是你也可以让它为python2工作。您可以通过删除
    kwargs
    中与孩子相关的条目,然后再将它们传递给家长

    代码(用于python3):

    Python代码2

    class Parent(object):
        def __init__(self, motherArg1, motherArg2=100):
            self.motherArg1 = motherArg1
            self.motherArg2 = motherArg2
        def printParent(self):
            print(self.motherArg1)
            print(self.motherArg2)
    
    class Child(Parent):
        def __init__(self, childArg1, *args, **kwargs): 
            # this shows the concept, it can be formulated more elegantly
            # with @Martijn Pieters answer's 'pop':
            if 'childArg2' in kwargs:
                childArg2 = kwargs['childArg2']
                del kwargs['childArg2']
            else:
                childArg2 = 2
            super(Child, self).__init__(*args, **kwargs)
            self.childArg1 = childArg1
            self.childArg2 = childArg2
        def printChild(self):
            print(self.childArg1)
            print(self.childArg2)
    
    child = Child(1, 3, childArg2=2, motherArg2=4)
    child.printChild()
    child.printParent()
    

    当您将父级的
    printChild
    重命名为
    printpparent
    (并修复
    print
    s)时,它已经在python 3中工作,如建议的:

    1
    2
    3
    4
    
    但是你也可以让它为python2工作。您可以通过删除
    kwargs
    中与孩子相关的条目,然后再将它们传递给家长

    代码(用于python3):

    Python代码2

    class Parent(object):
        def __init__(self, motherArg1, motherArg2=100):
            self.motherArg1 = motherArg1
            self.motherArg2 = motherArg2
        def printParent(self):
            print(self.motherArg1)
            print(self.motherArg2)
    
    class Child(Parent):
        def __init__(self, childArg1, *args, **kwargs): 
            # this shows the concept, it can be formulated more elegantly
            # with @Martijn Pieters answer's 'pop':
            if 'childArg2' in kwargs:
                childArg2 = kwargs['childArg2']
                del kwargs['childArg2']
            else:
                childArg2 = 2
            super(Child, self).__init__(*args, **kwargs)
            self.childArg1 = childArg1
            self.childArg2 = childArg2
        def printChild(self):
            print(self.childArg1)
            print(self.childArg2)
    
    child = Child(1, 3, childArg2=2, motherArg2=4)
    child.printChild()
    child.printParent()
    

    在Python 2中,必须将
    *args
    参数放在任何显式关键字参数之后:

    def __init__(self, childArg1, childArg2=100, *args, **kwargs):
    
    >>> class Parent(object):
    ...     def __init__(self, motherArg1, motherArg2=100):
    ...         self.motherArg1 = motherArg1
    ...         self.motherArg2 = motherArg2
    ...     def printParent(self):
    ...         print self.motherArg1
    ...         print self.motherArg2
    ...
    >>> class Child(Parent):
    ...     def __init__(self, childArg1, *args, **kwargs):
    ...         childArg2 = kwargs.pop('childArg2', 2)
    ...         super(Child, self).__init__(*args, **kwargs)
    ...         self.childArg1 = childArg1
    ...         self.childArg2 = childArg2
    ...     def printChild(self):
    ...         print self.childArg1
    ...         print self.childArg2
    ...
    >>> child = Child(1, 3, childArg2=2, motherArg2=4)
    >>> child.printChild()
    1
    2
    >>> child.printParent()
    3
    4
    
    但是,由于
    childArg2
    参数捕获了其他位置参数,因此不能使用这些参数:

    >>> child = Child(1, 3, childArg2=2, motherArg2=4)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __init__() got multiple values for keyword argument 'childArg2'
    
    这使得
    childArg2
    仅用作显式关键字参数,而
    *args
    捕获所有其他位置参数:

    def __init__(self, childArg1, childArg2=100, *args, **kwargs):
    
    >>> class Parent(object):
    ...     def __init__(self, motherArg1, motherArg2=100):
    ...         self.motherArg1 = motherArg1
    ...         self.motherArg2 = motherArg2
    ...     def printParent(self):
    ...         print self.motherArg1
    ...         print self.motherArg2
    ...
    >>> class Child(Parent):
    ...     def __init__(self, childArg1, *args, **kwargs):
    ...         childArg2 = kwargs.pop('childArg2', 2)
    ...         super(Child, self).__init__(*args, **kwargs)
    ...         self.childArg1 = childArg1
    ...         self.childArg2 = childArg2
    ...     def printChild(self):
    ...         print self.childArg1
    ...         print self.childArg2
    ...
    >>> child = Child(1, 3, childArg2=2, motherArg2=4)
    >>> child.printChild()
    1
    2
    >>> child.printParent()
    3
    4
    

    在Python 2中,必须将
    *args
    参数放在任何显式关键字参数之后:

    def __init__(self, childArg1, childArg2=100, *args, **kwargs):
    
    >>> class Parent(object):
    ...     def __init__(self, motherArg1, motherArg2=100):
    ...         self.motherArg1 = motherArg1
    ...         self.motherArg2 = motherArg2
    ...     def printParent(self):
    ...         print self.motherArg1
    ...         print self.motherArg2
    ...
    >>> class Child(Parent):
    ...     def __init__(self, childArg1, *args, **kwargs):
    ...         childArg2 = kwargs.pop('childArg2', 2)
    ...         super(Child, self).__init__(*args, **kwargs)
    ...         self.childArg1 = childArg1
    ...         self.childArg2 = childArg2
    ...     def printChild(self):
    ...         print self.childArg1
    ...         print self.childArg2
    ...
    >>> child = Child(1, 3, childArg2=2, motherArg2=4)
    >>> child.printChild()
    1
    2
    >>> child.printParent()
    3
    4
    
    但是,由于
    childArg2
    参数捕获了其他位置参数,因此不能使用这些参数:

    >>> child = Child(1, 3, childArg2=2, motherArg2=4)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __init__() got multiple values for keyword argument 'childArg2'
    
    这使得
    childArg2
    仅用作显式关键字参数,而
    *args
    捕获所有其他位置参数:

    def __init__(self, childArg1, childArg2=100, *args, **kwargs):
    
    >>> class Parent(object):
    ...     def __init__(self, motherArg1, motherArg2=100):
    ...         self.motherArg1 = motherArg1
    ...         self.motherArg2 = motherArg2
    ...     def printParent(self):
    ...         print self.motherArg1
    ...         print self.motherArg2
    ...
    >>> class Child(Parent):
    ...     def __init__(self, childArg1, *args, **kwargs):
    ...         childArg2 = kwargs.pop('childArg2', 2)
    ...         super(Child, self).__init__(*args, **kwargs)
    ...         self.childArg1 = childArg1
    ...         self.childArg2 = childArg2
    ...     def printChild(self):
    ...         print self.childArg1
    ...         print self.childArg2
    ...
    >>> child = Child(1, 3, childArg2=2, motherArg2=4)
    >>> child.printChild()
    1
    2
    >>> child.printParent()
    3
    4
    

    请在问题本身中包含正确格式的内容。在这种情况下,很明显你会得到什么样的输出,但a是一个好习惯。请在问题本身中包含这一点,格式正确。在这种情况下,很明显您会得到什么输出,但a是一个好习惯。因为OP使用的是显式
    object
    print
    语句,更不用说报告他们的代码不工作了,很明显他们使用的是2.x。告诉他们它在3.x中工作不是很有用,你是对的。我现在添加了一个Python2版本不,你说它可以在Python3上工作,所以也许我更喜欢更改我的python版本。非常感谢。由于OP使用的是显式
    对象
    打印
    语句,更不用说报告他们的代码不起作用了,很明显他们使用的是2.x。告诉他们它在3.x中工作不是很有用,你是对的。我现在添加了一个Python2版本不,你说它可以在Python3上工作,所以也许我更喜欢更改我的python版本。非常感谢。