Skip to content

Instantly share code, notes, and snippets.

@oze4
Last active January 22, 2019 19:07
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 oze4/3a8a801a882e499ee505b1f96be091ec to your computer and use it in GitHub Desktop.
Save oze4/3a8a801a882e499ee505b1f96be091ec to your computer and use it in GitHub Desktop.
class Person:
# this is a private class attr
__private_class_attr = "I am a private class attr"
# this is a class wide attribute ***********************************
public_class_wide_attribute = 0
# this is what allows you to instantiate objects - aka construct them
# which is why its called a constructor
def __init__(self):
# this is an instance attribute *********************************
self.instance_attribute = 0
# this is a private instance attribute ****************************
# the 'dunder' or double underscore at the beginning is what makes it private
self.__private_instance_attr = "I am a private instance attr"
# this is what allows us to grab the private instance attr
def get_private_instance_attr(self):
return self.__private_instance_attr
# this is what allow us to grab the private class attr, which can be static since it does not rely on any instance
@staticmethod
def get_private_class_attr():
return Person.__private_class_attr
# instantiate objects
nick = Person()
matt = Person()
# cant access private attr publicly unless you create a way to do so from within the class
#print(nick.__private_instance_attr) # *** REMOVE THE # AT THE FRONT OF THIS LINE TO SEE ERROR *****
# here is how you access private instance attr, publicly
print(nick.get_private_instance_attr())
print()
# here is how you access private class attr, from either an instance or statically from the class
print(nick.get_private_class_attr()) # access from an instance
print(Person.get_private_class_attr()) # access from class
print()
# show class attr on both instantiated objects (Person)
print(f'should be "0":')
print(f'class attr: nick: {nick.public_class_wide_attribute} is the same as matt: {matt.public_class_wide_attribute}')
print()
# change public class attr on nick, also changes on matt
Person.public_class_wide_attribute = 99
print(f'both should be 99, this attribute belongs to the class Person, not each instance')
print(f'should be 99: nick: {nick.public_class_wide_attribute} is the same as matt: {matt.public_class_wide_attribute}')
print()
# instance attributes
print(f'original instance attributes will be the same: nick:{nick.instance_attribute} matt:{matt.instance_attribute}')
# change instance attribute
matt.instance_attribute = 150
nick.instance_attribute = 300
print(f'instance attributes now different: nick: {nick.instance_attribute} matt: {matt.instance_attribute}')
# OUTPUT:
"""
I am a private instance attr
I am a private class attr
I am a private class attr
should be "0":
class attr: nick: 0 is the same as matt: 0
both should be 99, this attribute belongs to the class Person, not each instance
should be 99: nick: 99 is the same as matt: 99
original instance attributes will be the same: nick:0 matt:0
instance attributes now different: nick: 300 matt: 150
Process finished with exit code 0
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment