Created
September 18, 2014 17:59
-
-
Save drmalex07/d80643716263bb18f346 to your computer and use it in GitHub Desktop.
An example of using __format__ magic method in Python. #python #format
This file contains 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
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