“不带参数(给出1个)”但我没有给出任何参数

时间:2020-12-19 23:19:24

I am new to Python and I have written this simple script:

我是Python的新手,我编写了这个简单的脚本:

#!/usr/bin/python3
import sys

class Hello:
    def printHello():
        print('Hello!')

def main():
    helloObject = Hello()
    helloObject.printHello()   # Here is the error

if __name__ == '__main__':
    main()

When I run it (./hello.py) I get the following error message:

当我运行它(./hello.py)时,我收到以下错误消息:

Traceback (most recent call last):
  File "./hello.py", line 13, in <module>
    main()
  File "./hello.py", line 10, in main
    helloObject.printHello()
TypeError: printHello() takes no arguments (1 given)

Why does Python think I gave printHello() an argument while I clearly did not? What have I done wrong?

为什么Python认为我给printHello()一个参数,而我显然没有?我做错了什么?

3 个解决方案

#1


40  

The error is referring to the implicit self argument that is passed implicitly when calling a method like helloObject.printHello(). This parameter needs to be included explicitly in the definition of an instance method. It should look like this:

该错误指的是在调用helloObject.printHello()等方法时隐式传递的隐式self参数。此参数需要显式包含在实例方法的定义中。它应该如下所示:

class Hello:
  def printHello(self):
      print('Hello!')

#2


6  

If you want printHello as instance method, it should receive self as argument always(ant python will pass implicitly) Unless you want printHello as a static method, then you'll have to use @staticmethod

如果你想将printHello作为实例方法,它应该始终接收self作为参数(ant python将隐式传递)除非你想将printHello作为静态方法,否则你将不得不使用@staticmethod

#!/usr/bin/python3
import sys

class Hello:
    def printHello(self):
        print('Hello!')

def main():
    helloObject = Hello()
    helloObject.printHello()   # Here is the error

if __name__ == '__main__':
    main()

As '@staticmethod'

#!/usr/bin/python3
import sys

class Hello:
    @staticmethod
    def printHello():
        print('Hello!')

def main():
    Hello.printHello()   # Here is the error

if __name__ == '__main__':
    main()

#3


6  

Calling a method on the instance of an object returns the object itself (usually self) to the object. For example, calling Hello().printHello() is the same as calling Hello.printHello(Hello()), which uses an instance of a Hello object as the first argument.

在对象实例上调用方法会将对象本身(通常是self)返回给对象。例如,调用Hello()。printHello()与调用Hello.printHello(Hello())相同,后者使用Hello对象的实例作为第一个参数。

Instead, define your printHello statement as def printHello(self):

而是将printHello语句定义为def printHello(self):

#1


40  

The error is referring to the implicit self argument that is passed implicitly when calling a method like helloObject.printHello(). This parameter needs to be included explicitly in the definition of an instance method. It should look like this:

该错误指的是在调用helloObject.printHello()等方法时隐式传递的隐式self参数。此参数需要显式包含在实例方法的定义中。它应该如下所示:

class Hello:
  def printHello(self):
      print('Hello!')

#2


6  

If you want printHello as instance method, it should receive self as argument always(ant python will pass implicitly) Unless you want printHello as a static method, then you'll have to use @staticmethod

如果你想将printHello作为实例方法,它应该始终接收self作为参数(ant python将隐式传递)除非你想将printHello作为静态方法,否则你将不得不使用@staticmethod

#!/usr/bin/python3
import sys

class Hello:
    def printHello(self):
        print('Hello!')

def main():
    helloObject = Hello()
    helloObject.printHello()   # Here is the error

if __name__ == '__main__':
    main()

As '@staticmethod'

#!/usr/bin/python3
import sys

class Hello:
    @staticmethod
    def printHello():
        print('Hello!')

def main():
    Hello.printHello()   # Here is the error

if __name__ == '__main__':
    main()

#3


6  

Calling a method on the instance of an object returns the object itself (usually self) to the object. For example, calling Hello().printHello() is the same as calling Hello.printHello(Hello()), which uses an instance of a Hello object as the first argument.

在对象实例上调用方法会将对象本身(通常是self)返回给对象。例如,调用Hello()。printHello()与调用Hello.printHello(Hello())相同,后者使用Hello对象的实例作为第一个参数。

Instead, define your printHello statement as def printHello(self):

而是将printHello语句定义为def printHello(self):