Skip to content

Instantly share code, notes, and snippets.

@kalebr
Created June 27, 2014 15:23
Show Gist options
  • Save kalebr/0268668b01aae9ca983a to your computer and use it in GitHub Desktop.
Save kalebr/0268668b01aae9ca983a to your computer and use it in GitHub Desktop.
Demo of python static class variable
import random
class Student():
x = 17
def __init__(self):
self.age = random.randint(0, 20)
s = Student()
print s.x # 17
Student.x = 21
print s.x # 21
# note x is not a member of the instance
print s.__dict__ # no x
t = Student()
print t.x # 21
Student.x = 42
print s.x, t.x # 42, 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment