Error:

今天写一段简单类定义python代码所遇到报错问题:TypeError: drive() takes 2 positional arguments but 3 were given

class Car:
    speed = 0
    def drive(self,distance):
        time = distance / self.speed
        print(time)
bike = Car()
bike.speed=60
bike.drive(60,80)

后经排查,才发现是类定义中 def drive(selef,distance) 方法中self参数得问题

现在让我们简单了解一下Python中self的基础信息:

self ,表示创建的类实例本身,方法内部,就可以把各种属性绑定到self,因为self就指向创建的实例本身。在创建实例的时候,就不能传入空的参数了,必须传入与方法匹配的参数,但self不需要传,Python解释器会自己把实例变量传进去。

所以有两种解决方法

方法一:只传一个参数,如果你想传两个参数,那就看方法二

class Car:
    speed = 0
    def drive(self,distance):
        time = distance / self.speed
        print(time)
bike = Car()
bike.speed=60
bike.drive(80)
class Car:
    speed = 0
    def drive(self,distance,speed):
        time = distance / speed
        print(time)
bike = Car()
bike.drive(80,50)

 Error:  今天写一段简单类定义python代码所遇到报错问题:TypeError: drive() takes 2 positional arguments but 3 were given代码如下  class Car: speed = 0 def drive(self,distance): time = dist...
typeError : c al l() takes 2 position al arguments but 3 were given @TOC在运行 python 程序中出现了参数个数报错的原因。 1、类中定义的函数未设置参数self; 2、已经初始化了一个与类名相同的函数,因此再调用类时,调用了该函数而非类你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。我们对Markdown编辑器进行
今天在用ColumnTransformer方法对数值变量和非数值变量进行合成转换的时候,掌柜选择对其中的类别特征用LabelEncoder()方法,结果出现报错如上????。但是换成OneHotEncoder(独热编码)就成功运行。 于是疑惑 为什么会出现这样的情况? 一搜发现Stackoverflow上面也有同样疑问的朋友,结合两位答主回答: 发现两个主要的原因为什么不适合用LabelEncoder(): 因为LabelEncoder()的transformer是用来编码目标变量(y)(可参见官方
num_pipeline = Pipeline([ ('selector', DataFrameSelector(num_attribs)), ('imputer', Imputer(strategy="median")),
其他问题参考: Python 基础 - TypeError takes 2 position al arguments but 3 were given https://blog.csdn.net/weixin_42716620/article/details/82888572 某个方法传进去过多的参数导致报错 问题背景: 在使用 python 的matplotlib模块的多个坐标系显示-plt.subplots(面向对象的画图方法)时,报错如下 TypeError : set_ticks() takes
在运行程序时,定位到以下语句出现错误:v al idate(test_loader, model, n_labels) 报错信息为:v al idate() takes 2 position al arguments but 3 were given if v al _f1 >= best_f1: best_f1 = v al _f1 test_output_scores, test_f1 = v al idate(test_loader, model, n_labels) al l_test_
TypeError at /add_atr/ add_atr() takes 0 position al arguments but 1 was given Request Method: GET Request URL: http://127.0.0.1:8000/add_stu/ Django Version: 3.0.3 Exception Type: TypeError Exception V al ue: add_atr() takes 0 position al arguments but 1 was given Exception Location: E:\env\django9\lib
异常描述: 执行以下代码 WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(By.XPATH, '//span[text()="全选"]')), PyCharm出现异常提示 TypeError : __init__() takes 2 position al arguments but 3 were given 原译:init()方法只取2个参数,但实际提供了3个参数。 presence_of_elemen
运行不成功啊,但我是mac环境。 web = urllib.request.urlopen('https://www.baidu.com') Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.[code=python] [/code] Redis学习笔记二 (BitMap算法分析与BitCount语法) 匿名用户: 我自己给我自己留言。哈哈哈 Redis学习笔记一(Redis的详细安装及Linux环境变量配置和启动) 匿名用户: 菊花满地残。