TypeError: method() takes 1 positional argument but 2 were given

前端 未结 9 1188
孤街浪徒
孤街浪徒 2020-11-22 05:02

If I have a class...

class MyClass:

    def method(arg):
        print(arg)

...which I use to create an object...

my_objec         


        
9条回答
  •  我在风中等你
    2020-11-22 05:46

    Newcomer to Python, I had this issue when I was using the Python's ** feature in a wrong way. Trying to call this definition from somewhere:

    def create_properties_frame(self, parent, **kwargs):
    

    using a call without a double star was causing the problem:

    self.create_properties_frame(frame, kw_gsp)
    

    TypeError: create_properties_frame() takes 2 positional arguments but 3 were given

    The solution is to add ** to the argument:

    self.create_properties_frame(frame, **kw_gsp)
    

提交回复
热议问题