[英]Constructor Arguments Error when Declaring an Instance of a Class
I was working on a class to control the power of two DC motors with the Raspberry Pi using the imported RPi.GPIO
library. 我正在上一个类,使用导入的
RPi.GPIO
库通过Raspberry Pi控制两个直流电动机的功率。 The code is as followed: 代码如下:
import RPi.GPIO as GPIO
class Motor:
def _init_(self, MotorPin):
self.MotorControlPin = MotorPin
GPIO.setmode(GPIO.BOARD)
GPIO.setup(self.MotorControlPin, GPIO.OUT)
self.PWM = GPIO.PWM(self.MotorControlPin, 100)
def SetPower(self, Power):
self.PWM.start(Power)
When I try to create an instance of the class, RightMotor = Motor(12)
Python returns the error Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> RightMotor = Motor(12) TypeError: this constructor takes no arguments
当我尝试创建该类的实例时,
RightMotor = Motor(12)
Python返回错误Traceback (most recent call last): File "<pyshell#23>", line 1, in <module> RightMotor = Motor(12) TypeError: this constructor takes no arguments
The python IDLE seems to think that the _init_(self, Motor)
function does not take any arguments. python IDLE似乎认为
_init_(self, Motor)
函数没有任何参数。 Am I using the function incorrectly? 我使用该功能不正确吗? If not, what is the problem?
如果没有,那是什么问题?
The name of the constructor method in Python is __init__
, with two underscores at the beginning and two at the end. Python中构造函数方法的名称为
__init__
,开头有两个下划线,结尾有两个下划线。 Your code defines _init_
instead, which as far as Python is concerned is just another ordinary method with no bearing on object construction. 您的代码改为定义
_init_
,就Python而言,它只是另一种与对象构造无关的普通方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.