Skip to content

Instantly share code, notes, and snippets.

@r3
Created August 31, 2012 04:30
Show Gist options
  • Save r3/3549083 to your computer and use it in GitHub Desktop.
Save r3/3549083 to your computer and use it in GitHub Desktop.
Classes and Instances
class Foo():
# It has an attribute called "class_attribute"
# All instances of 'Foo' will share (have a reference to) this class_attribute
# We are not in a method, so we have no reference to any instances, only the class
class_attribute = None
# This is the special method used when instantiating 'Foo'
# Here, 'self' has a special meaning ONLY because of the way it is called.
# As the first parameter, it will be given the created instance of 'Foo'
# Realize that the word 'self' has no special meaning. It is simply a parameter.
# We could just as easily have called it 'this' or 'that' and it would still...
# ...contain the implicitly passed reference to the new 'Foo' instance
# Again, 'self' is not a keyword and is not special. It's just convention...
# ...to call it that.
def __init__(self, var):
# Here we give that instance an attribute: 'var'
# This is an instance attribute. 'Foo' doesn't have one...
# ...because 'Foo' isn't an instance of 'Foo'
self.var = var
@classmethod
# Don't worry about the decorator above too much, it simply means that...
# ...the method deals with the class instead of an instance.
# When we call a class method, a reference to the class (in this case 'Foo') will...
# ...be passed implicitly instead of a reference to the new instance (like 'bar' or 'baz)
def modify_class_attrib(cls, new_contents):
# Notice that the first parameter is 'cls' and not 'self'? That is because this is...
# ...a class method.
# The class of the object is implicitly passed instead of a reference to the instance.
# We could have called it whatever we wanted, but 'cls' is convention
# This will change the 'class_attribute' for the 'Foo' class to the new_contents argument
cls.class_attribute = new_contents
# These are instances of 'Foo'
bar = Foo(23)
baz = Foo(7)
# They both have access to the 'class_attribute' of the 'Foo' class
# The 'is' keyword below implies that they are the same (not identical) object
print(bar.class_attribute is baz.class_attribute) # => True
# Here, we create an instance variable of the same name ('class_attribute')
# This "obscures" (overrides) the access to the 'Foo' attribute of the same name
bar.class_attribute = 12
# Neither 'Foo' nor the 'baz' instance will be changed as we're only affecting the 'bar' instance
print(bar.class_attribute is baz.class_attribute) # => False
# Here we call a method that "belongs" to the class. It affects only the class
Foo.modify_class_attrib(92)
# But since the 'baz' instance still has a reference to 'Foo' attribute 'class_attribute'...
# ...it now points to the new object (which is still a class attribute, not an instance attribute)
print(baz.class_attribute) # => 92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment