Why does Python mix OO concepts and non OO concepts for operation s on basic types?

Andrew Dalke dalke at dalkescientific.com
Wed May 22 19:34:24 EDT 2002


Michael P. Soulier:
>    Python _does_ seem mildly inconsistent in this regard. Giving strings
>methods helped a bit, but tuples still have none, and neither do numbers.
>
>>>> dir(())
>[]
>>>> dir(2)
>[]
>
>    IMHO, they should all be objects with methods and a class, such that
they
>may be subclassed.

Upgrade to a newer version of Python.  First, what 'dir' does was never
explicitly defined, and its implementation has since changed.  Second,
one of the big changes in 2.2 was to make type subclassable.

>>> dir(2)
['__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__']
>>> dir( () )
['__add__', '__class__', '__contains__', '__delattr__', '__eq__', '__ge__',
'__get
attribute__', '__getitem__', '__getslice__', '__gt__', '__hash__',
'__init__', '__
le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__repr_
_', '__rmul__', '__setattr__', '__str__']
>>>

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list