Attributeerror: 'tuple' Object Has No Attribute 'read'

Errors are an essential office of a programmer's life. And it is not at all bad if you become an fault. Getting error means yous are learning something new. But we need to solve those errors. And earlier solving that error, nosotros should know why nosotros are getting that error. At that place are some commonly occurred errors in python similar Type Error, Syntax Error, Key Error, Attribute mistake, Name Error, and so on.

In this article, we will learn almost what is python Attribute Error, why we go information technology, and how we resolve it? Python interpreter raises an Aspect Fault when we try to call or access an attribute of an object, but that object does not possess that attribute. For instance- If nosotros try using upper() on an integer, we volition get an attribute mistake.

Why we Get Attribute Mistake?

Whenever we try to access an aspect that is not possessed past that object, we get an aspect fault. For example- We know that to make the string uppercase, we use the upper().

Output-

AttributeError: 'int' object has no aspect 'upper'

Hither, we are trying to catechumen an integer to an upper case letter, which is not possible every bit integers do not attribute being upper or lower. Only if try using this upper() on a string, we would have got a upshot because a cord can exist qualified as upper or lower.

Some Common Mistakes which result in Aspect error in python

If we try to perform append() on whatsoever information type other than List:

Sometimes when nosotros desire to concatenate two strings we effort appending one string into another, which is not possible and we get an Attribute Mistake.

string1="Ashwini"  string2="Mandani"  string1.append(string2)          

Output-

          AttributeError: 'str' object has no attribute 'suspend'

Same goes with tuples,

a=tuple((v,6))  a.suspend(vii)          

Output-

AttributeError: 'tuple' object has no attribute 'append'

Trying to access attribute of Grade:

Sometimes, what nosotros do is that we endeavor to access attributes of a class which information technology does not possess. Let united states meliorate understand information technology with an case.

Hither, nosotros have two classes- Ane is Person class and the other is Vehicle class. Both possess different properties.

class Person:      def __init__(self,age,gender,name):         cocky.age=age         self.gender=gender         self.name=proper name      def speak(self):          print("Hello!! How are you?")   class Vehicle:      def __init__(self , model_type , engine_type):          cocky.model_type = model_type          self.engine_type = engine_type      def horn(self):          print("beep!! beep")   ashwini=Person(20,"male","ashwini")  print(ashwini.gender)  impress(ashwini.engine_type)          

Output-

male   AttributeError: 'Person' object has no attribute 'engine_type'
AttributeError: 'Person' object has no attribute 'horn'
car=Vehicle( "Hatchback" , "Petrol" )  print(auto.engine_type)  print(car.gender)          

Output-

Petrol  AttributeError: 'Vehicle' object has no aspect 'gender'
          Error-          AttributeError: 'Vehicle' object has no aspect 'speak'

In the above examples, when we tried to access the gender holding of Person Class, nosotros were successful. But when we tried to access the engine_type() attribute, information technology showed u.s.a. an error. It is considering a Person has no attribute called engine_type. Similarly, when we tried calling engine_type on Vehicle, we were successful, merely that was not in the instance of gender, as Vehicle has no attribute called gender.

AttributeError: 'NoneType'

We become NoneType Error when we get 'None' instead of the instance we are supposing we will go. Information technology means that an assignment failed or returned an unexpected result.

name=None i=v if i%two==0:     proper name="ashwini" name.upper()          

Output-

          AttributeError: 'NoneType' object has no attribute 'upper'

While working with Modules:

It is very mutual to encounter an attribute fault while working with modules. Suppose, we are importing a module named hello and trying to access two functions in it. One is print_name() and some other is print_age().

Module Hello-

def print_name():     print("Hello! The proper noun of this module is module1")  import hello  hello.print_name() hello.print_age()          

Output-

Hello! The name of this module is module1  AttributeError: module 'hello' has no attribute 'print_age'

As the module hi does non contain print_age attribute, we got an Aspect error. In the next section, we volition learn how to resolve this fault.

How to Resolve Attribute Fault in Python

Use help():

The developers of python have tried to solve any possible problem faced past Python programmers. In this case, too, if we are getting dislocated, that whether a particular aspect belongs to an object or not, we tin make use of help(). For example, if we don't know whether nosotros tin utilise append() on a string, nosotros tin print(help(str)) to know all the operations that we can perform on strings. Not only these built-in data types, but we can likewise employ help() on user-defined information types like Form.

For example- if nosotros don't know what attributes does class Person that we declared above has,

          impress(help(Person))                  

Output-

python attribute error

Isn't it cracking! These are precisely the attributes nosotros defined in our Person grade.

At present, let us effort using assistance() on our hullo module inside the hi module.

Help on module hello: NAME hello FUNCTIONS print_name()

Using Try – Except Statement

A very professional person way to tackle not simply Attribute error but any mistake is by using endeavor-except statements. If nosotros think nosotros might get an error in a particular block of code, we tin can enclose them in a endeavor cake. Let us run across how to exercise this.

Suppose, we are not sure whether Person class comprise engine_type attribute or not, we tin enclose it in try block.

class Vehicle:      def __init__(self , model_type , engine_type):          self.model_type = model_type          self.engine_type = engine_type      def horn(cocky):          impress("beep!! beep")   car=Vehicle( "Hatchback" , "Petrol" )   try:     print(car.engine_type)     impress(car.gender)   except Exception every bit e:     print(e)          

Output-

Petrol  'Vehicle' object has no attribute 'gender'.

Must Read

  • How to Catechumen String to Lowercase in
  • How to Summate Square Root
  • User Input | Input () Function | Keyboard Input
  • Best Book to Learn Python

Conclusion

Whenever to try to access an aspect of an object that does non belong to it, nosotros become an Aspect Error in Python. We tin tackle it using either help() office or attempt-except statements.

Try to run the programs on your side and let us know if you lot have any queries.

Happy Coding!

watermanfreg1972.blogspot.com

Source: https://www.pythonpool.com/attribute-error-python/

0 Response to "Attributeerror: 'tuple' Object Has No Attribute 'read'"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel