Skip to content

Instantly share code, notes, and snippets.

@moea
Created June 11, 2014 00:34
Show Gist options
  • Save moea/31c4a8ea8c4f79306430 to your computer and use it in GitHub Desktop.
Save moea/31c4a8ea8c4f79306430 to your computer and use it in GitHub Desktop.
interfaces sketch
from zope.interface import implements, Interface
class ILimitedProfile(Interface):
"""
Marker interface for basic profile functionality.
"""
def doLimited():
pass
class IFullProfile(ILimitedProfile):
"""
Marker interface for full profile functionality.
"""
def doFull():
pass
class LimitedUser:
implements(ILimitedProfile)
def doLimited(self):
return 'limited :('
class FullUser(LimitedUser):
implements(IFullProfile)
def doFull(self):
return 'full!'
def doSomething(user):
if IFullProfile.providedBy(user):
return user.doFull()
# they both implement ILimitedProfile as FullUser inherits the
# implementation.
return user.doLimited()
if __name__ == '__main__':
print doSomething(LimitedUser())
print doSomething(FullUser())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment