Created
May 29, 2019 10:16
-
-
Save CodersArts/c4a2f3e1e3c8290ab4691a11164c6dfa to your computer and use it in GitHub Desktop.
How to declare static variable in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parent: # define parent class | |
parentAttr = 100 # Static variable or class variable declaration | |
def __init__(self): | |
print ("Calling parent constructor") | |
def parentMethod(self): | |
print ('Calling parent method') | |
# Set Class or static variable 'parentAttr' | |
def setAttr(self, attr): | |
Parent.parentAttr = attr | |
def getAttr(self): | |
print ("Parent attribute :", Parent.parentAttr) | |
class Child(Parent): # Inherite parent class attribute by child class | |
def __init__(self): | |
print ("Calling child constructor") | |
def childMethod(self): | |
print ('Calling child method') | |
c = Child() # instance of child | |
c.childMethod() # child calls its method | |
c.parentMethod() # calls parent's method | |
c.setAttr(200) # again call parent's method | |
c.getAttr() # again call parent's method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment