TypeError:method()占用1个位置参数,但给出了2个

时间:2014-05-29 23:27:33

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

如果我有课......

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告诉我我给了它两个参数,当时我只给了一个?

10 个答案:

答案 0 :(得分:237)

在Python中,这个:

my_object.method("foo")

...是syntactic sugar,解释器将幕后翻译成:{/ p>

MyClass.method(my_object, "foo")

......正如你所看到的,确实有两个论点 - 从调用者的角度来看,第一个是隐含的。

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

class MyNewClass:

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

如果您在method("foo")的实例上致电MyNewClass,它会按预期运作:

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

偶尔(但不经常),您真的关心您的方法所绑定的对象,在这种情况下,您可以decorate使用该方法内置staticmethod()函数如此说明:

class MyOtherClass:

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

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

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

答案 1 :(得分:10)

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

我遇到了此错误消息,发现这篇文章很有帮助。在我的情况下,我已经覆盖了一个 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可能)。

当然,这是一个“不带参数”的TypeError,在Python中的对象初始化方面,它与期望一个“得到两个”没什么不同。

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

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

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

答案 2 :(得分:3)

当你没有指定 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))

答案 3 :(得分:2)

如其他答案所述-使用实例方法时,您需要传递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

答案 4 :(得分:2)

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

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

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

这个故事的寓意:在睡眠不足的情况下编程是一场失败的战斗。

答案 5 :(得分:1)

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

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

答案 6 :(得分:0)

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)

答案 7 :(得分:0)

就我而言,我忘记添加ng new my-app

我正在这样调用方法

()

但是应该是这样

obj = className.myMethod

答案 8 :(得分:-2)

在您的情况下创建类 MyClass 的对象时,请不要在为对象定义类型后包含括号。

代码应该是..

my_object = MyClass

不是,

my_object = MyClass()

之后,调用方法是一样的...

my_object.method("foo")

答案 9 :(得分:-3)

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

@classmethod
def test(cls):
    return ''