Skip to content

Instantly share code, notes, and snippets.

@tobie
Created March 13, 2009 04:02
Show Gist options
  • Save tobie/78424 to your computer and use it in GitHub Desktop.
Save tobie/78424 to your computer and use it in GitHub Desktop.
Python repr of common types
repr(False)
#-> False
repr(True)
#-> True
repr(None)
#-> None
repr('foo')
#-> 'foo'
repr(123)
#-> 123
repr(123.4)
#-> 123.40000000000001
import re
r = re.compile('foo')
repr(r)
#-> <_sre.SRE_Pattern object at 0x22758>
import time
from datetime import date
repr(date.today())
#-> datetime.date(2009, 3, 13)
repr([0, 1, [2, 3]])
#-> [0, 1, [2, 3]]
repr({'foo': 123})
#-> {'foo': 123}
# self-containing arrays
a = [1, 2, 3]
a.append(a)
repr(a)
#-> [1, 2, 3, [...]]
# self-containing hashes
h = {'foo': 123}
h['bar'] = h
repr(h)
#-> {'foo': 123, 'bar': {...}}
class Foo:
def __init__(self):
self.foo = 123
self.bar = 456
repr(Foo())
#-> <__main__.Foo instance at 0x64260>
repr(Foo)
#-> <class __main__.Foo at 0x51a20>
repr([].reverse)
#-> <built-in method reverse of list object at 0x50738>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment