Skip to content

Instantly share code, notes, and snippets.

@Zabanaa
Created November 14, 2016 13:05
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 Zabanaa/3ec39955226009f860915e5d8088bcd4 to your computer and use it in GitHub Desktop.
Save Zabanaa/3ec39955226009f860915e5d8088bcd4 to your computer and use it in GitHub Desktop.
Composition vs Inheritance
# Main client class
# Makes the credentials available to the other classes
# In this scenario, User uses or has an API, but it isn't an API
# So we don't really need to inherit from the client class (Dribbble_API)
class Dribbble_API(object):
def __init__(self, client_id=None, client_secret=None, access_token=None):
self.client_id = client_id
self.client_secret = client_secret
self.access_token = access_token
def call_api(self, endpoint):
print("{}&access_token={}".format(endpoint, self.access_token))
# Simple User class
# Does not need to inherit from the client / does not need to know about the credentials
# We just pass an instance of Dribbble_API to User, and access the properties like so
# self.client.property
class User():
def __init__(self, client):
self.client = client
def get_info(self):
return self.client.call_api('/user')
d = Dribbble_API(client_id="my_client_id", client_secret="MyClientSecret", access_token="mysecrettoken")
u = User(d)
u.get_info() # prints /user&access_token=mysecrettoken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment