Created
June 11, 2014 00:34
-
-
Save moea/31c4a8ea8c4f79306430 to your computer and use it in GitHub Desktop.
interfaces sketch
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
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