如果我有课……
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告诉我我给了它两个参数,而我只给了一个?
当你没有指定__init__()或任何其他方法寻找的参数的no时,就会发生这种情况。
例如:
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))
正如在其他回答中提到的那样——当您使用实例方法时,需要将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