Super keyword in python

technical talkiess
0

 Super keyword :- 


class A:

    classA = "You are in class A"


    def __init__(self):

        self.name = "A"

        self.age = "45"

        self.this_variable = "Test A"

       

# inheritance of a  class

class B(A):

    classB = "You are in class B"

    def __init__(self):

        # agar aap derivedd class ke variable ko use karna chate ho to aap super ka use karke base class ke constructor ko call ka sakte hai ..... agar apne isko upper or function ke start me use kiya aur uske baad kuch variable ko override kiya to variable new vale print hoge .... but apne isko variable declare ke bad likha to apko saab base class ke variable milenge

        super().__init__()

        self.name = "B"

        self.age = "86"



a = A()

b = B()

# agar aap without super class ke isko run karte hai to apko ye wala error ayenga

# Traceback (most recent call last):

#   File "c:\Users\ukv-208\Downloads\prac\python\oops.py", line 21, in <module>

#     print(b.this_variable)

#           ^^^^^^^^^^^^^^^

# AttributeError: 'B' object has no attribute 'this_variable'

# PS C:\Users\ukv-208\Downloads\prac>

# Solution :- Apko agar isko hide aur access karna hai to aap khali super key word ka use karke solve kar sakte hai aur access kar sakte hai


print(b.this_variable)


Tags

Post a Comment

0Comments

Thanks you for commenting your questions. I will see question and respond you.

Post a Comment (0)