Attributeerror: 'str' Object Has No Attribute 'read' Json

Errors are an essential part of a programmer's life. And it is non at all bad if you get an mistake. Getting fault means yous are learning something new. But we need to solve those errors. And before solving that error, we should know why we are getting that error. There are some commonly occurred errors in python similar Type Mistake, Syntax Error, Key Mistake, Attribute error, Proper noun Error, and then on.

In this article, we will learn about what is python Aspect Error, why we get it, and how we resolve it? Python interpreter raises an Attribute Error when we try to call or access an attribute of an object, only that object does not possess that attribute. For case- If we attempt using upper() on an integer, we volition get an attribute error.

Why we Become Attribute Error?

Whenever nosotros try to access an attribute that is non possessed by that object, we get an attribute error. For instance- We know that to make the cord uppercase, we utilise the upper().

Output-

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

Here, nosotros are trying to convert an integer to an upper case letter, which is not possible as integers do not aspect being upper or lower. Simply if endeavor using this upper() on a string, nosotros would have got a consequence because a string can be qualified as upper or lower.

Some Common Mistakes which result in Attribute fault in python

If we attempt to perform append() on any data type other than List:

Sometimes when we want to concatenate two strings we try appending one string into some other, which is not possible and we become an Aspect Fault.

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

Output-

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

Same goes with tuples,

a=tuple((5,6))  a.append(7)          

Output-

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

Trying to access attribute of Form:

Sometimes, what we exercise is that we try to access attributes of a class which information technology does not possess. Let the states better understand it with an example.

Here, we have two classes- 1 is Person class and the other is Vehicle class. Both possess different properties.

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

Output-

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

Output-

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

In the above examples, when we tried to access the gender property of Person Class, nosotros were successful. Simply when we tried to admission the engine_type() attribute, it showed us an mistake. It is considering a Person has no attribute called engine_type. Similarly, when we tried calling engine_type on Vehicle, we were successful, but that was not in the instance of gender, equally Vehicle has no attribute called gender.

AttributeError: 'NoneType'

Nosotros become NoneType Error when we get 'None' instead of the example nosotros are supposing we will get. It means that an assignment failed or returned an unexpected effect.

name=None i=five if i%ii==0:     proper noun="ashwini" proper noun.upper()          

Output-

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

While working with Modules:

It is very common to encounter an attribute error while working with modules. Suppose, we are importing a module named hello and trying to admission two functions in it. One is print_name() and another is print_age().

Module How-do-you-do-

def print_name():     impress("Hullo! The name of this module is module1")  import howdy  hello.print_name() how-do-you-do.print_age()          

Output-

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

Equally the module hello does not incorporate print_age aspect, we got an Attribute error. In the next section, nosotros will learn how to resolve this error.

How to Resolve Aspect Fault in Python

Utilise help():

The developers of python accept tried to solve any possible problem faced past Python programmers. In this case, besides, if nosotros are getting dislocated, that whether a particular attribute belongs to an object or not, we can make utilize of aid(). For instance, if we don't know whether we can use append() on a string, nosotros can print(help(str)) to know all the operations that we can perform on strings. Not simply these built-in data types, simply we can too use help() on user-defined data types like Form.

For example- if we don't know what attributes does class Person that we alleged to a higher place has,

          print(help(Person))                  

Output-

python attribute error

Isn't it great! These are precisely the attributes we defined in our Person class.

Now, let us try using assist() on our hello module within the hi module.

Help on module hi: NAME hello FUNCTIONS print_name()

Using Endeavor – Except Statement

A very professional way to tackle not just Attribute error but whatever mistake is past using effort-except statements. If we think we might get an error in a particular block of code, we can enclose them in a try block. Let us see how to practice this.

Suppose, we are non certain whether Person class contain engine_type attribute or non, we can enclose it in try block.

form Vehicle:      def __init__(self , model_type , engine_type):          cocky.model_type = model_type          self.engine_type = engine_type      def horn(self):          print("beep!! beep")   car=Vehicle( "Hatchback" , "Petrol" )   try:     print(car.engine_type)     print(auto.gender)   except Exception equally e:     print(due east)          

Output-

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

Must Read

  • How to Convert Cord to Lowercase in
  • How to Calculate Square Root
  • User Input | Input () Function | Keyboard Input
  • Best Book to Acquire Python

Conclusion

Whenever to try to access an attribute of an object that does non belong to it, we become an Attribute Mistake in Python. Nosotros tin tackle it using either assistance() function or try-except statements.

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

Happy Coding!

lewisthaversonly.blogspot.com

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

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

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel