相关疑难解决方法(0)

为什么不能在子类的__init__中传递*args和**kwargs

为了理解*args和**kwargs我做了一些搜索,当我遇到这个问题时*args和**kwargs?

所选答案下面的答案引起了我的注意,这是:

class Foo(object):
    def __init__(self, value1, value2):
    # do something with the values
        print value1, value2

class MyFoo(Foo):
    def __init__(self, *args, **kwargs):
    # do something else, don't care about the args
        print 'myfoo'
        super(MyFoo, self).__init__(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

我在这个例子上尝试了一些东西并以这种方式运行代码:

class Foo(object):
    def __init__(self, value1, value2):
    # do something with the values
        print 'I think something is being called here'
        print value1, value2


class MyFoo(Foo):
    def __init__(self, *args, **kwargs):
    # do something else, don't care about the args
        print args, kwargs …
Run Code Online (Sandbox Code Playgroud)

python

14
推荐指数
3
解决办法
3万
查看次数

标签 统计

python ×1