5

i have a question. In oops using python, super can access the method or constructor , but not the attributes . Why is that??

class Phone:

  def __init__(self, price, brand, camera):
    print ("Inside phone constructor")
    self.__price = price
    self.brand = brand
    self.camera = camera

def buy(self):
    print ("Buying a phone")

def return_phone(self):
    print ("Returning a phone")

class FeaturePhone(Phone):
    pass

class SmartPhone(Phone):
    def __init__(self, price, brand, camera, os, ram):
       super().__init__(price, brand, camera)
       self.os = os
      self.ram = ram
      print ("Inside smartphone constructor")

    def buy(self):
      print(super().camera) #Error
      print ("Buying a smartphone")

s=SmartPhone(20000, "Samsung", 12, "Android", 2)

print(s.buy()) #error
print(s.brand)

Can anyone please explain?? If possible then how?

2 Answers 2

3

First I thought - it's obvious. And, then, it is actually an interesting question, and I learned something new today.

super() doesn't return the instantiated class object. It returns an instance of "super" class that takes the class "SmartPhone" and "SmartPhone object" as arguments. It then runs this closure.

As such, you cannot get to the attributes of super class on the value of super() because it's an instance of "super" class.

https://docs.python.org/3.7/library/functions.html#super

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Naoyuki Tai, i got it about the calling part and taking arguments.but if it returns instance of super class,then it means that it returns phone class object . So it should have access to the attributes of phone class like normal objects do.
If you really want to access the value, (a) you can use normal "self.FOO" (b) you can do so by calling function super().__getitem__(). Please read rhettinger.wordpress.com/2011/05/26/super-considered-super
0

You don't have to access the parent class to use one of its methods. Your class-SmartPhone inherits from the Phone class which allows you to access its attributes and methods using self. So you only need to call self.buy() to be able to use the method

class Phone:
  def __init__(self, price, brand, camera):
    print ("Inside phone constructor")
    self.__price = price
    self.brand = brand
    self.camera = camera
  def buy(self):
      print ("Buying a phone")
  def return_phone(self):
      print ("Returning a phone")

class FeaturePhone(Phone):
    pass

class SmartPhone(Phone):
    def __init__(self, price, brand, camera, os, ram):
      super().__init__(price, brand, camera)
      self.os = os
      self.ram = ram
      print ("Inside smartphone constructor")
    def buy(self):
      print(self.camera) 
      print ("Buying a smartphone")

s=SmartPhone(20000, "Samsung", 12, "Android", 2)

print(s.buy()) # this works
print(s.brand)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.