在面向对象编程的时候会报这样的错误:
TypeError: func() missing 1 required positional argument: 'XXXXX'
报这个错误有两种原因:
1.实例化的时候类名后没写上括号
class P:
def __init__(self):
pass
def func(self,content):
print(content)
p=P
p.func("hello")
执行结果:TypeError: func2() missing 1 required positional argument: 'content'
因为P没有带括号:所以代码加上括号后就正常调用了
class P:
def __init__(self):
pass
def func(self,content):
print(content)
p=P()
p.func("hello")
#执行结果:
hello
2.定义了类但是没有实例化就调用了即:类名.方法名
class P:
def __init__(self):
pass
def func(self,content):
print(content)
P.func("hello")
执行结果:
TypeError: func() missing 1 required positional argument: 'content'
综上所述:定义了类之后调用类要进行实例化,调用时使用实例化名.方法名