Python AttributeError: 'tuple' 对象没有属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17290114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:48:16  来源:igfitidea点击:

AttributeError: 'tuple' object has no attribute

pythonpython-2.7

提问by Alok

I'm a beginner in python. I'm not able to understand what the problem is?

我是python的初学者。我无法理解问题是什么?

def list_benefits():

        s1 = "More organized code"
        s2 = "More readable code"
        s3 = "Easier code reuse"
        s4 = "Allowing programmers to share and connect code together"
        return s1,s2,s3,s4

def build_sentence():

        obj=list_benefits()
        print obj.s1 + " is a benefit of functions!"
        print obj.s2 + " is a benefit of functions!"
        print obj.s3 + " is a benefit of functions!"

print build_sentence()

The error I'm getting is:

我得到的错误是:

Traceback (most recent call last):
   Line 15, in <module>
   print build_sentence()
   Line 11, in build_sentence
   print obj.s1 + " is a benefit of functions!"
AttributeError: 'tuple' object has no attribute 's1'

采纳答案by Aswin Murugesh

You return four variables s1,s2,s3,s4 and receive them using a single variable obj. This is what is called a tuple, objis associated with 4 values, the values of s1,s2,s3,s4. So, use index as you use in a list to get the value you want, in order.

您返回四个变量 s1,s2,s3,s4 并使用单个变量接收它们obj。这就是所谓的 a tupleobj与 4 个值相关联,即 的值s1,s2,s3,s4。因此,在列表中使用 index 来按顺序获取所需的值。

obj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"
print obj[3] + " is a benefit of functions!"

回答by Ignacio Vazquez-Abrams

You're returning a tuple. Index it.

你正在返回一个tuple. 索引它。

obj=list_benefits()
print obj[0] + " is a benefit of functions!"
print obj[1] + " is a benefit of functions!"
print obj[2] + " is a benefit of functions!"

回答by Patashu

Variables names are only locally meaningful.

变量名称仅在局部有意义。

Once you hit

一旦你打

return s1,s2,s3,s4

return s1,s2,s3,s4

at the end of the method, Python constructs a tuple with the values of s1, s2, s3 and s4 as its four members at index 0, 1, 2 and 3 - NOT a dictionary of variable names to values, NOT an object with variable names and their values, etc.

在该方法的末尾,Python 构造了一个元组,其中 s1、s2、s3 和 s4 的值作为它在索引 0、1、2 和 3 处的四个成员 - 不是变量名称到值的字典,不是具有变量的对象名称及其值等。

If you want the variable names to be meaningful after you hit returnin the method, you must create an object or dictionary.

如果您希望在您点击return方法后变量名称有意义,您必须创建一个对象或字典。

回答by Enis Bilgin

class list_benefits(object):
    def __init__(self):
        self.s1 = "More organized code"
        self.s2 = "More readable code"
        self.s3 = "Easier code reuse"

def build_sentence():
        obj=list_benefits()
        print obj.s1 + " is a benefit of functions!"
        print obj.s2 + " is a benefit of functions!"
        print obj.s3 + " is a benefit of functions!"

print build_sentence()

I know it is late answer, maybe some other folk can benefit If you still want to call by "attributes", you could use class with default constructor, and create an instance of the class as mentioned in other answers

我知道这是迟到的答案,也许其他一些人可以受益

回答by nanakweku

I am working in python flask: I had the same problem... There was a "," after I declared my my form variables; I am working with wtforms. That is what caused all the confusion

我在 python flask 中工作:我遇到了同样的问题......在我声明我的表单变量之后有一个“,”;我正在使用 wtforms。这就是造成所有混乱的原因