Home > OS >  Not able to print value of object
Not able to print value of object

Time:06-22

class Freelencer:
    company="google"
    level=0
    def upgradelevel(self):
        self.level=self.level 1
    
class Employee:
    company="visa"
    ecode=120
    
class Programmer(Freelencer,Employee):
    name="rohit"
    
p=Programmer()
p.upgradelevel()
print(p)

I want to print value of level is changed to 1. Below is the workflow of the code. enter image description here

CodePudding user response:

you are printing the object. you need to point "self.level".

print(p.level)

CodePudding user response:

In either the Freelencer or Programmer class, you need to define a __str__ method that returns the string that should be shown when you print() the object. You could write a method as simple as this:

def __str__(self):
    return 'level '   str(self.level)

Then print(p) will show

level 1

CodePudding user response:

You might need to override the print method in the Programmer class. use

def __repr__(self):
print(self.name) # or whatever you want to print
  • Related