Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?


1 Answers

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'
like image 176
Wasi Ahmad Avatar answered Nov 05 '25 16:11

Wasi Ahmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!