Search code examples
pythonclassattributeerror

AttributeError: 'NoneType' object has no attribute


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?


Solution

  • 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'