0

How can you do this in an object oriented programming python what to print what is in the return of str method ?

class Bike():

    def __str__(self):
        return Components.__str__(self)+", "+ self.getFrameType()

    def printComponents(self):
        print(Bike.self())
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97

1 Answers1

1

The __str__ method is invoked when you want to convert it to string.

class Bike():
    def __init__(self):
       self.hello = "hello"

    def __str__(self):
        return self.hello + " goodbye"

>>> print(str(Bike()))
"hello goodbye"

If you intend to print the object directly i.e. with print(Bike()) you'll want to use __repr__ instead. See Confused about __str__ in Python for more details.

personjerry
  • 1,045
  • 8
  • 28