-1

I run the following code:

class mobile:
    def __init__(self, name, brand):
        self.name = name
        self.brand = brand
    def get_name(self):
        return self.name
class cpu(mobile):
    def get_name(self):
        print("This mobile has HighTech %s CPU " % self.name)    
brand = cpu("Intel","Sony")
print(brand.get_name())

The result is:

This mobile has HighTech Intel CPU
None

Why does it return 'None' in second line?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
peacekeeper
  • 45
  • 1
  • 6
  • 1
    You're calling the `get_name` method of `cpu` class that is polymorph and does not return anything, so by default, it returns `None`. Just do not print `brand.get_name()` or make `return "This mobile has HighTech %s CPU " % self.name`. – Artyom Vancyan Aug 09 '22 at 06:37

1 Answers1

1

The print statement when calling the get_name function is redunant, try:

class mobile:
    def __init__(self, name, brand):
        self.name = name
        self.brand = brand
    def get_name(self):
        return self.name
class cpu(mobile):
    def get_name(self):
        print("This mobile has HighTech %s CPU " % self.name)    
brand = cpu("Intel","Sony")
brand.get_name()

Inside get_name() you already state a print statement, therefore no second print outside the function is necessary.

Pi.Lilac
  • 135
  • 1
  • 7