Created
November 15, 2017 16:21
-
-
Save anacampesan/bd11f5cf8d027170cc2f25ba4c6ec7b7 to your computer and use it in GitHub Desktop.
OOP in Python
This file contains 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 User: | |
role = 'user' | |
def __init__(self, name): | |
self.name = name | |
def get_name(self): | |
print(self.name) | |
@classmethod | |
def get_role(self): | |
print(self.role) | |
# Instance methods | |
a = User('my custom username') | |
a.get_name() | |
# Class methods | |
User.get_role() | |
# Class variables | |
print(User.role) | |
# Calling or accessing instance methods and variables as static throws an error | |
# User.get_name() | |
# print(User.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment