Last active
February 12, 2019 06:32
-
-
Save harrisonmalone/3e98829f2832cfaf5eb2f76a6287cc49 to your computer and use it in GitHub Desktop.
basic class and objects example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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