码迷,mamicode.com
首页 > 其他好文 > 详细

处理TypeError: testFunc() missing 1 required positional argument: 'self' -- 没有实例化对象的错误

时间:2020-01-07 15:59:57      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:使用   miss   实例化   --   处理   none   error   fun   required   

在Python中,使用类分两步:

  1. 应该先对类进行实例化;

  2. 然后在应用类。注意,实例化的过程是应该待括号的。

# -*-coding: utf-8 -*-

‘‘‘在Python中,应该先对类进行实例化,然后在应用类。注意,实例化的过程是应该待括号的。
‘‘‘
class TestClass:
    def __init__(self):
        print(‘In init‘)
    def testFunc(self):
        print(‘In testFunc‘)

‘‘‘
ERROR1:
#错误方式:未实例化
print(TestClass.testFunc())
TypeError: testFunc() missing 1 required positional argument: ‘self‘


ERROR2:
#错误方式,TestClass:未带括号
testInstance = TestClass
print(testInstance.testFunc())
TypeError: testFunc() missing 1 required positional argument: ‘self‘
‘‘‘

#正确方式,TestClass():带着括号
testInstance = TestClass()
print(testInstance.testFunc())

in init
In test testFunc
None

  

处理TypeError: testFunc() missing 1 required positional argument: 'self' -- 没有实例化对象的错误

标签:使用   miss   实例化   --   处理   none   error   fun   required   

原文地址:https://www.cnblogs.com/Barrybl/p/12161922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!