Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active February 12, 2019 06:32
Show Gist options
  • Save harrisonmalone/3e98829f2832cfaf5eb2f76a6287cc49 to your computer and use it in GitHub Desktop.
Save harrisonmalone/3e98829f2832cfaf5eb2f76a6287cc49 to your computer and use it in GitHub Desktop.
basic class and objects example
class Dog():
total_dogs = 0
def __init__(self, name):
self.name = name
# every time you initialize you add to the class variable
Dog.total_dogs += 1
def speak(self):
# you need to pass self as a parameter for instance methods
return 'woof!'
@staticmethod
# if you have @classmethod you have to pass Dog as a parameter
def count_dogs():
return Dog.total_dogs
tilly = Dog('tilly')
ruby = Dog('ruby')
print(vars(tilly))
# with vars you get back a dictionary of object attributes
print(tilly.speak())
print(Dog.count_dogs())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment