|
发表于 2020-9-12 12:14:57
|
显示全部楼层
- class CeShi():
- bagua=10
- def __init__(self,a):
- self.a = a
- def lai(self):
- print(self.a)
- print(CeShi.a)
- print(bagua)
- c=CeShi(5)
- c.lai()
复制代码
C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\python.exe D:/python/test/1.py
5
Traceback (most recent call last):
File "D:/python/test/1.py", line 12, in <module>
c.lai()
File "D:/python/test/1.py", line 8, in lai
print(CeShi.a)
AttributeError: type object 'CeShi' has no attribute 'a'
Process finished with exit code 1
self.a能用
CeShi.a报错了,没有说能用啊,没实例化对象前,self就表示自身吧,没说这样也能用吧
至于这个bagua,就是局部变量和全局变量的问题了,会报错:bagua未定义 |
|