Modifying the {} and [] tokens

Andrew Dalke adalke at
Sun Aug 24 05:38:15 CEST 2003

Geoff Howland:
> BTW, some of the other comments in my team have been for a desire of
> more inclusive OO, such as [].len() instead of len([]), and this sort
> of thing.

As others have pointed out, you can use obj.__len__() if you really
want to use it as a method.

I admit this looks ugly.  Hoever, there is a benefit to this approach.
I am free to define any methods I want and any attributes on want,
so long as the names don't both start and end with "__", which is
reserved for the language.

Compare this to, say, Ruby, where it takes extra knowledge to
know that 'initialize', 'id', 'method', 'to_a', 'hash', and others have
special meanings.  Suppose I had a HashBrown controller, for
a machine for making hash browns.  I couldn't do the equivalent

possible_methods = ["soft", "crunchy", "southwestern"]

class AbstractHashBrownController:
    def __init__(self, method, interface):
        assert method in possible_methods
        self.method = method = interface.acquire_machine_identifier()

    def initialize(self):
        """Initialize hash-brown systems (eg, light the gas)"""

    def hash(self):
        """chop up the potatoes"""

While this example is a bit extreme, it worries me that I need
to remember all the methods names reserved by the language,
and that if the base Object adds new methods then my code
might accidentally break in consequence.

Most languages don't need that many special variables,
so I find it appropriate that when they are needed then I
need to do a bit extra work for them.  At a different extreme
is Perl, where builtins don't have a leading sigil while variables
do (or can), so if I have $hash then the language can add 'hash'
and not affect anything.  I find that too extreme because most
things are user-defined code, so it makes the common case
more complicated.

Python then gets rid of the problem by defining builtins for
accessing those __special__ methods, so they are rarely
needed except for defining.  (Though it then gains the problem
that you need to know id uses __id__, bool uses __len__
and __nonzero__, + uses __add__ and __radd__ and
__coerce__, etc.)

                    dalke at

More information about the Python-list mailing list