Answers for "get variable from parent class python"

5

how to get the parent class using super python

class Foo(Bar):

    def __init__(self, *args, **kwargs):
        # invoke Bar.__init__
        super().__init__(*args, **kwargs)
Posted by: Guest on August-13-2020
0

how to access parent class attribute from child class in python

# I've been searching for this for so long so i tought this might help you out.

class Person: #Parent class
  def __init__(self, fname, lname):
    self.fname = fname
    self.lname = lname
    
  def display(self):
    '''To check if parent method works'''
    print(self.fname)
    print(self.lname)
    
class Subscriber(Person): #Child class
  def __init__(self, fname, lname, ID):
    self.ID = ID
    
    #The part below this comment is the most important
    Person.__init__(self, fname, lname)
    #Make sure all arguments of the Parent class are in the Person.__init__()
    
  def displayID(self):
  '''To see if child method works'''
    print(self.ID)
	
John = Subscriber("John", "Doe", 1)
John.display()

#OUPUT:
#John
#Doe

John.displayID()
#OUTPUT
#1
Posted by: Guest on May-11-2021

Code answers related to "get variable from parent class python"

Python Answers by Framework

Browse Popular Code Answers by Language