1
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

Sumit
  • 21
  • 4
  • It would be better and easier for the community to answer your question if you ask in a more clear way and also format your code in a readable way. – Amir Jun 22 '22 at 04:03
  • Done please check now – Sumit Jun 22 '22 at 04:09
  • please check this https://stackoverflow.com/questions/30318638/pythons-magic-method-for-representation-of-class – Amir Jun 22 '22 at 04:11

3 Answers3

1

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

print(p.level)

1

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
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
  • i done writing the function you provided but now it shows Programmer is not defined – Sumit Jun 22 '22 at 04:47
  • If `Programmer` is not defined, you probably didn't run all the code you showed. Try running your whole code block, with a `__str__` method added to one of those classes. Then it should work OK. – Matthias Fripp Jun 22 '22 at 04:51
0

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
Henry Kam
  • 11
  • 1
  • why i am not able to print the object value just with print method ? it gives me location of object in o/p i.e <__main__.Programmer object at 0x000002A00130BBB0> – Sumit Jun 22 '22 at 04:12