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?

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • 1
    `name` is an attribute of the instance, so you should do `a.b()` and in a second step `print(a.name)`. – Matthias Dec 09 '16 at 07:35

2 Answers2

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'
Community
  • 1
  • 1
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
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
Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78