[Tutor] OOP programming principles overview? [Complex
numbers and OOP]
Kirby Urner
urnerk@qwest.net
Wed, 21 Nov 2001 09:20:41 -0800
Interestingly, as of Python 2.2, you can take ordinary integers
and study them as objects:
>>> dir(3)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__float__', '__floordiv__',
'__getattribute__', '__hash__', '__hex__', '__init__', '__int__',
'__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__',
'__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__',
'__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__',
'__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__',
'__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
'__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__',
'__truediv__', '__xor__']
>>> 3 .__add__
<method-wrapper object at 0x01300FB0>
>>> 4 .__add__
<method-wrapper object at 0x013044E0>
>>> 5 .__add__(6)
11
Notice you need to add a space after the integer, before the .
-- otherwise the parser wouldn't understand you don't mean
float.
Such as space is legal even in ordinary circumstances:
>>> class A:
property = 'ddd'
>>> a = A()
>>> a.property
'ddd'
>>> a .property
'ddd'
Kirby