python TypeError: missing 1 required positional argument:'self'

简介: python TypeError: missing 1 required positional argument:'self'

python TypeError: missing 1 required positional argument:'self'


Python 调用类的函数时报错如下:

TypeError: seperate_data() missing 1 required positional argument:'self'

报错原因:

train_data, test_data =  DataCleaner.seperate_data()

DataCleaner 是个类,seperate_data() 是其中的方法,不能直接这样调用,需要先将类实例化。

代码改为:

train_data, test_data =  DataCleaner().seperate_data()

注意:DataCleaner 是类,DataCleaner() 是类的对象。

代码原理:

调用 object 的函数(如下边的  A().t() )的时候会自动把自己作为第一个参数传进去,就是 self,而直接通过 类(就是不加括号的方式)调用函数不会,所以会报错。见下图:

解释:

A是 class 也就是类

A() 是object 也就是对象

A.t() 就是调用类的方法

A().t()就是调用对象的方法

以上,问题解决~

相关文章
|
Python
python类型错误(TypeError)
【7月更文挑战第13天】
842 9
|
Python
python类型错误(TypeError)
【7月更文挑战第20天】
685 4
|
Python
|
JSON 安全 数据格式
7-6|python报错TypeError: can't pickle _thread.RLock objects
7-6|python报错TypeError: can't pickle _thread.RLock objects
|
Web App开发 测试技术 API
【Python】已解决:TypeError: *init*() got an unexpected keyword argument ‘firefox_options’
【Python】已解决:TypeError: *init*() got an unexpected keyword argument ‘firefox_options’
467 0
|
8月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1296 102
|
8月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
464 104
|
8月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
360 103
|
8月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
373 82

推荐镜像

更多