如果我有课……

class MyClass:

    def method(arg):
        print(arg)

... 我用来创建一个对象…

my_object = MyClass()

... 我调用方法(“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告诉我我给了它两个参数,而我只给了一个?


当前回答

在Python中,这是:

my_object.method("foo")

... 是语法糖,翻译人员在幕后将其翻译成:

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

偶尔(但不是经常),你真的不关心你的方法绑定的对象,在这种情况下,你可以用内置的staticmethod()函数来装饰方法:

class MyOtherClass:

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

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

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

其他回答

简单地说

在Python中,你应该将self作为类中所有定义方法的第一个参数:

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

然后你可以根据你的直觉使用你的方法:

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

为了更好地理解,你也可以阅读这个问题的答案:自我的目的是什么?

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

class MyClass:

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

MyClass.method("i am a static method")

将cls参数传递给@classmethod来解决这个问题。

@classmethod
def test(cls):
    return ''

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

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

如果方法是Static,则不传递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

当我睡眠不足时,我会得到这个错误,并使用def而不是class创建一个类:

def MyClass():
    def __init__(self, x):
        self.x = x

a = MyClass(3)
-> TypeError: MyClass() takes 0 positional arguments but 1 was given