-2

I want to print name as tanya but since self.name = None has been assigned in constructor it is printing None. So how to get tanya printed when the check function gets called:

class A:
    def __init__(self):
        self.name = None

    def price(self):
        self.name = "tanya"

    def check(self):
        print(self.price())

a=A()
a.check()
quant
  • 2,184
  • 2
  • 19
  • 29
lavanya
  • 1
  • 1

2 Answers2

1

The constructor isn't the problem

print(self.price()) is going to print None because you are printing the result of the function.

It then sets self.name="tanya" after printing, but you are ignoring it

Instead, I think you want

a=A()
a.price()
print(a.name)

and forget the check function

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
class A:
    def __init__(self):
        self.name=None

    def price(self):
        self.name="tanya"
        return self.name

    def check(self):
        print(self.price())

a=A()
a.check()

You just have to add the return statement in the price function, and you're done!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Sandesh34
  • 279
  • 1
  • 2
  • 14