【问题标题】:TypeError: method() takes 1 positional argument but 2 were givenTypeError:method() 接受 1 个位置参数,但给出了 2 个
【发布时间】:2014-07-19 15:02:39
【问题描述】:

如果我有课...

class MyClass:

    def method(arg):
        print(arg)

...我用来创建对象...

my_object = MyClass()

...我打电话给method("foo") 就像这样...

>>> my_object.method("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method() takes exactly 1 positional argument (2 given)

...为什么 Python 告诉我我给了它两个参数,而我只给了一个参数?

【问题讨论】:

  • 该消息有很多原因;这里的具体原因是所有实例方法都需要一个第一个参数,我们自定义称为self。所以声明def method(arg):对于一个方法来说是错误的,应该是def method(self, arg):。当方法 dispatch 尝试调用 method(arg): 并匹配两个参数 self, arg 时,您会收到该错误。

标签: python python-3.x methods arguments self


【解决方案1】:

在 Python 中,这个:

my_object.method("foo")

...是syntactic sugar,解释器在后台将其翻译成:

MyClass.method(my_object, "foo")

...正如你所见,它确实有两个参数——只是从调用者的角度来看,第一个参数是隐含的。

这是因为大多数方法都对它们被调用的对象做一些工作,因此需要有某种方法可以在方法内部引用该对象。按照惯例,第一个参数在方法定义中称为self

class MyNewClass:

    def method(self, arg):
        print(self)
        print(arg)

如果您在 MyNewClass 的实例上调用 method("foo"),它会按预期工作:

>>> my_new_object = MyNewClass()
>>> my_new_object.method("foo")
<__main__.MyNewClass object at 0x29045d0>
foo

偶尔(但不经常),你真的关心你的方法绑定到的对象,在这种情况下,你可以decorate带有内置@987654323的方法@函数这么说:

class MyOtherClass:

    @staticmethod
    def method(arg):
        print(arg)

...在这种情况下,您不需要在方法定义中添加self 参数,它仍然有效:

>>> my_other_object = MyOtherClass()
>>> my_other_object.method("foo")
foo

【讨论】:

  • 简而言之:将self 作为方法的第一个参数添加可以解决问题。
  • @amoebe - 我的类型
  • 或者在定义它之前添加@staticmethod。酷
【解决方案2】:

简单来说。

在 Python 中,您应该将 self 参数添加为类中所有已定义方法的第一个参数:

class MyClass:
  def method(self, arg):
    print(arg)

那么你就可以按照你的直觉使用你的方法了:

>>> my_object = MyClass()
>>> my_object.method("foo")
foo

这应该可以解决你的问题:)

为了更好的理解,你也可以阅读这个问题的答案:What is the purpose of self?

【讨论】:

  • 这个答案有什么问题?为什么有人给她负分?毕竟,它是问题的答案,并且与其他答案相比,它的简单性与众不同,这对于一些正在寻找答案的人来说可能很重要。不是吗?
【解决方案3】:

遇到此类错误时需要考虑的其他事项:

我遇到了这个错误消息,发现这篇文章很有帮助。结果在我的例子中,我覆盖了一个有对象继承的__init__()

继承的例子比较长,所以我会跳到一个更简单的不使用继承的例子:

class MyBadInitClass:
    def ___init__(self, name):
        self.name = name

    def name_foo(self, arg):
        print(self)
        print(arg)
        print("My name is", self.name)


class MyNewClass:
    def new_foo(self, arg):
        print(self)
        print(arg)


my_new_object = MyNewClass()
my_new_object.new_foo("NewFoo")
my_bad_init_object = MyBadInitClass(name="Test Name")
my_bad_init_object.name_foo("name foo")

结果是:

<__main__.MyNewClass object at 0x033C48D0>
NewFoo
Traceback (most recent call last):
  File "C:/Users/Orange/PycharmProjects/Chapter9/bad_init_example.py", line 41, in <module>
    my_bad_init_object = MyBadInitClass(name="Test Name")
TypeError: object() takes no parameters

PyCharm 没有发现这个错字。 Notepad++ 也没有(其他编辑器/IDE 可能)。

当然,这是一个“不带参数”的类型错误,就 Python 中的对象初始化而言,它与期望一个时的“有两个”没有太大区别。

解决主题:如果语法正确,将使用重载初始化程序,但如果不正确,它将被忽略并使用内置函数。对象不会期望/处理这个并且会抛出错误。

在语法错误的情况下:修复很简单,只需编辑自定义的init语句:

def __init__(self, name):
    self.name = name

【讨论】:

  • 这里没有SyntaxError。代码在语法上是正确的;它只是正确地定义了一个名为___init__ 的方法,没有人会调用它,而不是特殊的方法__init__。这就是没有检测到错误的原因——因为没有可检测的错误。
【解决方案4】:

Python 新手,当我以错误的方式使用 Python 的 ** 功能时,我遇到了这个问题。试图从某个地方调用这个定义:

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

使用没有双星的调用会导致问题:

self.create_properties_frame(frame, kw_gsp)

TypeError: create_properties_frame() 接受 2 个位置参数,但给出了 3 个

解决方案是在参数中添加**

self.create_properties_frame(frame, **kw_gsp)

【讨论】:

  • 来自 JavaScript,我希望能够将列表或字典作为参数传递。但这似乎不是 Python 的工作方式:您必须使用 * 或 ** 来解构列表,后者用于关键字参数列表。
  • @LittleBrain:是的,如果您希望将列表(或字典)视为函数内的列表(/字典),您可以传递一个列表(或字典)。但是,如果您希望将它们的各个值解包并与函数中的 args (/kwargs) 匹配,那么您需要 *args (/**kwargs) 语法。
【解决方案5】:

正如其他答案中提到的 - 当您使用实例方法时,您需要将 self 作为第一个参数传递 - 这是错误的根源。

除此之外,重要的是要了解只有实例方法将self 作为第一个参数才能引用实例

如果方法是静态,则您不传递self,而是传递cls 参数(或class_)。

请看下面的例子。

class City:

   country = "USA" # This is a class level attribute which will be shared across all instances  (and not created PER instance)

   def __init__(self, name, location, population):
       self.name       = name
       self.location   = location
       self.population = population
 
   # This is an instance method which takes self as the first argument to refer to the instance 
   def print_population(self, some_nice_sentence_prefix):
       print(some_nice_sentence_prefix +" In " +self.name + " lives " +self.population + " people!")

   # This is a static (class) method which is marked with the @classmethod attribute
   # All class methods must take a class argument as first param. The convention is to name is "cls" but class_ is also ok
   @classmethod
   def change_country(cls, new_country):
       cls.country = new_country

一些测试只是为了让事情更清楚:

# Populate objects
city1 = City("New York",    "East", "18,804,000")
city2 = City("Los Angeles", "West", "10,118,800")

#1) Use the instance method: No need to pass "self" - it is passed as the city1 instance
city1.print_population("Did You Know?") # Prints: Did You Know? In New York lives 18,804,000 people!

#2.A) Use the static method in the object
city2.change_country("Canada")

#2.B) Will be reflected in all objects
print("city1.country=",city1.country) # Prints Canada
print("city2.country=",city2.country) # Prints Canada

【讨论】:

    【解决方案6】:

    当您没有指定 __init__() 或任何其他方法寻找的参数时,就会发生这种情况。

    例如:

    class Dog:
        def __init__(self):
            print("IN INIT METHOD")
    
        def __unicode__(self,):
            print("IN UNICODE METHOD")
    
        def __str__(self):
            print("IN STR METHOD")
    
    obj=Dog("JIMMY",1,2,3,"WOOF")
    

    当你运行上面的程序时,它会给你一个这样的错误:

    TypeError: __init__() 接受 1 个位置参数,但给出了 6 个

    我们怎样才能摆脱这个东西?

    只要传参数,__init__()方法找什么

    class Dog:
        def __init__(self, dogname, dob_d, dob_m, dob_y, dogSpeakText):
            self.name_of_dog = dogname
            self.date_of_birth = dob_d
            self.month_of_birth = dob_m
            self.year_of_birth = dob_y
            self.sound_it_make = dogSpeakText
    
        def __unicode__(self, ):
            print("IN UNICODE METHOD")
    
        def __str__(self):
            print("IN STR METHOD")
    
    
    obj = Dog("JIMMY", 1, 2, 3, "WOOF")
    print(id(obj))
    

    【讨论】:

      【解决方案7】:

      我在睡眠不足时收到此错误,并使用def 而不是class 创建一个类:

      def MyClass():
          def __init__(self, x):
              self.x = x
      
      a = MyClass(3)
      -> TypeError: MyClass() takes 0 positional arguments but 1 was given
      
      

      故事的寓意:在剥夺睡眠的情况下编程是一场失败的战斗。

      【讨论】:

        【解决方案8】:

        如果你想在不创建对象的情况下调用方法,你可以将方法更改为静态方法。

        class MyClass:
        
            @staticmethod
            def method(arg):
                print(arg)
        
        MyClass.method("i am a static method")
        

        【讨论】:

          【解决方案9】:

          您实际上应该创建一个类:

          class accum:
              def __init__(self):
                  self.acc = 0
              def accumulator(self, var2add, end):
                  if not end:
                      self.acc+=var2add
              return self.acc
          

          【讨论】:

            【解决方案10】:

            就我而言,我忘记添加()

            我是这样调用方法的

            obj = className.myMethod
            

            但应该是这样的

            obj = className.myMethod()
            

            【讨论】:

              【解决方案11】:

              cls 参数传递给@classmethod 以解决此问题。

              @classmethod
              def test(cls):
                  return ''
              

              【讨论】:

              • 我开始使用 OOP 并坚持使用这个示例。我将感谢帮助解决它 class Catalogo: def __init__(self): self.peliculas = [] def agregar(self, pelicula): self.peliculas.append(pelicula) def mostrar(self): for pelicula in self.peliculas : print(pelicula) # Print toma por defecto str(peicula) pelicula1 = Catalogo ("El Padrino") pelicula2 = Catalogo ("El Padrino: Parte 2") #Catalogo.agregar("El Padrino") #Catalogo.agregar( "El Padrino: Parte 2") # Añadimos otra Catalogo.mostrar()
              猜你喜欢
              • 2016-10-13
              • 2019-04-20
              • 2018-11-15
              • 2016-08-09
              • 2020-01-30
              • 2019-08-02
              • 1970-01-01
              相关资源
              最近更新 更多