Slide 76
Slide 76 text
Copyright (C) 2009, David Beazley, http://www.dabeaz.com
2-
Playing with Special Methods
• If you use dir() on built-in objects, you will
see a lot of the special methods defined
76
>>> x = 42
>>> dir(x)
['__abs__', '__add__', '__and__', '__class__', '__cmp__',
'__coerce__', '__delattr__', '__div__', '__divmod__',
'__doc__', '__float__', '__floordiv__', '__format__',
'__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__index__', '__init__', '__int__',
...]
>>> x.__add__(10)
52
>>> x.__str__()
'52'
>>> x.__hex__()
'0x2a'
>>>
If you're inclined, you can
call them directly (although
you wouldn't normally do
this in most programs)