Skip to content

Instantly share code, notes, and snippets.

@cjerdonek
Created June 23, 2012 11:27
Show Gist options
  • Save cjerdonek/2977954 to your computer and use it in GitHub Desktop.
Save cjerdonek/2977954 to your computer and use it in GitHub Desktop.
Sample code for Pystache issue #123
# 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