4

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
  • 1
    name is an attribute of the instance, so you should do a.b() and in a second step print(a.name). Commented Dec 9, 2016 at 7:35

2 Answers 2

6

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'
Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.