Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyCoolClass(object):
- def __init__(self, name):
- self.name = name
- foo = MyCoolClass("jordan")
- print(foo.name)
- try:
- foo.nothere
- except AttributeError:
- print("oops, that didn't work")
- ## Output:
- ## jordan
- ## oops, that didn't work
- class MyCoolerClass(object):
- def __init__(self, name):
- self.name = name
- def __getitem__(self, item):
- return "I dunno what %s is" % item
- def __getattr__(self, attr):
- return "you want whaat? %s??" % attr
- foo = MyCoolerClass("vishal")
- print(foo.name)
- print(foo.nothere)
- print(foo[1])
- ## Output:
- ## vishal
- ## you want whaat? nothere??
- ## I dunno what 1 is
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement