Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'tuple' object has no attribute

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

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' 
like image 319
Alok Avatar asked Jun 25 '13 05:06

Alok


People also ask

How do you fix tuple object has no attribute?

The Python "AttributeError: 'tuple' object has no attribute" occurs when we access an attribute that doesn't exist on a tuple. To solve the error, use a list instead of a tuple to access a list method or correct the assignment.

What does object has no attribute mean in Python?

It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.

Is a tuple an object?

A tuple is a collection of objects which ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

What is tuple error in Python?

The Python "TypeError: 'tuple' object is not callable" occurs when we try to call a tuple as if it were a function. To solve the error, make sure to use square brackets when accessing a tuple at a specific index, e.g. my_tuple[0] .


1 Answers

You return four variables s1,s2,s3,s4 and receive them using a single variable obj. This is what is called a tuple, obj is 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.

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!" 
like image 116
Aswin Murugesh Avatar answered Sep 22 '22 02:09

Aswin Murugesh