Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Created September 18, 2014 17:59
Show Gist options
  • Save drmalex07/d80643716263bb18f346 to your computer and use it in GitHub Desktop.
Save drmalex07/d80643716263bb18f346 to your computer and use it in GitHub Desktop.
An example of using __format__ magic method in Python. #python #format
class Foo(object):
def __init__(self, x):
self._x = x
def __format__(self, format_spec):
return '<Foo x={0}>'.format(self._x)
foo = Foo(12)
# Pass "baz" as a format_spec
print 'This is a foo: {0:baz}'.format(foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment