AttributeError: 'NoneType' object has no attribute

23,953

Solution 1

NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. That usually means that an assignment or function call up above failed or returned an unexpected result. See reference.

So, you can do something like this.

class asas(object):
    def b(self):
        self.name = "Berkhan"
        return self.name

a = asas()
print(a.b()) # prints 'Berkhan'

or

class asas(object):
    def b(self):
        self.name = "Berkhan"
        return self

a = asas()
print(a.b().name) # prints 'Berkhan'

Solution 2

b() returns nothing. Therefore it returns None.

You probably want something like this:

class asas(object):
    def b(self):
        self.name = "Berkhan"
        return self
a = asas()
a.b().name
Share:
23,953
Admin
Author by

Admin

Updated on April 21, 2021

Comments

  • Admin
    Admin about 3 years

    I'm work with python and I need a function in class like

    class asas(object):
        def b(self):
            self.name = "Berkhan"
    a = asas()
    a.b().name
    

    and I check this module

    Traceback (most recent call last):
      File "C:\Users\Berkhan Berkdemir\Desktop\new 1.py", line 5, in <module>
        a.b().name
    AttributeError: 'NoneType' object has no attribute 'name'
    

    What should I do?