Skip to main content
 首页 » 编程设计

python之需要帮助理解为什么我得到属性错误

2025年05月04日20lautakyan007

我刚开始从 Eric Matthes 的 python 速成类学习 python,它一直告诉我我有一个属性错误

motor_bikes = 'harley', 'Fz07', 'Crouch rocket' 
 
last_owned = motor_bikes.pop() 
 
print(last_owned) 

假设从列表中弹出最后一个索引,以便我可以在新变量 lasted_owned 中使用它

请您参考如下方法:

您正在创建一个 tuple , 不是 list .元组是不可变的,它们没有 .pop()。该错误从字面上告诉您:

AttributeError: 'tuple' object has no attribute 'pop' 
motor_bikes = ['harley', 'Fz07', 'Crouch rocket']  # this is a muteable list 
 
last_owned = motor_bikes.pop()  # it has a pop method 
 
print(last_owned) 

作品:

Crouch rocket 

您通过以下方式创建元组:

t1 = 1,3,4 
t2 = 1, 
t3 = (1,2,3,) 
 
print(t1,t2,t3)  # (1, 3, 4), (1,), (1, 2, 3)  -all tuples