-2

Code Output

I'm not able to set value to class variable in parent class but able to print it.It is saying attribute doesn't exist even when i am able to print it.

CODE:

class Base(object): 
    x=20

class Derived(Base):

    def __init__(self):
        print(super().x)
        super().x=80

obj=Derived()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    What are you actually trying to achieve here? `Base.x` is a class attribute, if you want to override that as a class attribute in Derived set it the same way. If you want to set an instance attribute, set it on self. – jonrsharpe Dec 30 '18 at 10:08
  • i am just poking around in python and i really need to know why i am getting value of x using super().x but not able to set any value to x using same, – Simranjeet Singh Dec 30 '18 at 10:35

1 Answers1

1

A Derived is a Base, i.e. all instances of Derived can be treated exactly like instances of Base. In your case, that means you simply set self.x = 80.

Mohammad Jafari
  • 1,742
  • 13
  • 17