Skip to content

Instantly share code, notes, and snippets.

@dskecse
Last active November 4, 2015 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dskecse/037de9c5cacba8f0f557 to your computer and use it in GitHub Desktop.
Save dskecse/037de9c5cacba8f0f557 to your computer and use it in GitHub Desktop.
Decorators in Python
def honorific(klass):
class HonorificClass(klass):
def full_name(self):
return 'Dr. ' + super(HonorificClass, self).full_name()
return HonorificClass
@honorific
class Person(object):
def __init__(self, first, last):
self.first = first
self.last = last
def full_name(self):
return ' '.join([self.first, self.last])
name = Person('Emmet', 'Brown').full_name()
print 'full name: {0}'.format(name) # full name: Dr. Emmet Brown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment