Skip to content

Instantly share code, notes, and snippets.

@CodersArts
Created May 29, 2019 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodersArts/c4a2f3e1e3c8290ab4691a11164c6dfa to your computer and use it in GitHub Desktop.
Save CodersArts/c4a2f3e1e3c8290ab4691a11164c6dfa to your computer and use it in GitHub Desktop.
How to declare static variable in python
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