Skip to content

Instantly share code, notes, and snippets.

@anacampesan
Created November 15, 2017 16:21
Show Gist options
  • Save anacampesan/bd11f5cf8d027170cc2f25ba4c6ec7b7 to your computer and use it in GitHub Desktop.
Save anacampesan/bd11f5cf8d027170cc2f25ba4c6ec7b7 to your computer and use it in GitHub Desktop.
OOP in Python
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