Skip to main content
 首页 » 编程设计

python之错误 "... object has no attribute ..."是什么意思

2024年10月24日9sharpest

考虑:

代码

class sand: 
    def __init__(self): 
        print("I am a pickle") 
        def next(self): 
            print("I am a tuner") 

测试并输出

>>> x = sand() 
I am a pickle 
>>> x.next() 
Traceback (most recent call last): 
  File "<pyshell#26>", line 1, in <module> 
    x.next() 
AttributeError: 'sand' object has no attribute 'next' 

请您参考如下方法:

没有属性 next 因为你的缩进不正确。使用:

class sand: 
    def __init__(self): 
        print("I am a pickle") 
 
    def next(self): 
        print("I am a tuner") 
 
x = sand() 
x.next() 

输出

I am a pickle 
I am a tuner