Created
June 23, 2012 11:27
-
-
Save cjerdonek/2977954 to your computer and use it in GitHub Desktop.
Sample code for Pystache issue #123
This file contains hidden or 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
# Sample code for: https://github.com/defunkt/pystache/issues/123 | |
class MyClass(object): | |
def __init__(self, data): | |
self.attr = 5 | |
self.data = data | |
def __getitem__(self, key): | |
return self.data[key] | |
def __getattr__(self, name): | |
return self.__getitem__(name) # or alternatively, return self.data[key] | |
def hello(self, text): | |
print "hello %s" % text | |
foo = MyClass({'bar': 'baz'}) | |
# Now all of these work. | |
print foo['bar'] | |
print foo.bar | |
print foo.attr | |
print foo.hello("world") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment