What's better about Ruby than Python?

Alex Martelli aleaxit at yahoo.com
Tue Aug 19 05:50:32 EDT 2003


Heiko Wundram wrote:
   ...
> class X:
> def test(*args):
> print args
> 
> X.test()      # 1
> x = X()
> x.test()      # 2
> X.test(x)     # 3
> 
> Run this, and for the first call you will get an empty tuple, while for

Wrong:

>>> class X:
...   def test(*args): print args
...
>>> X.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unbound method test() must be called with X instance as first 
argument (got nothing instead)
>>>

X.test is not a function -- it's an unbound method; to call it, you
MUST therefore pass it at least one argument, and the first argument
you pass to it must be an isntance of X (or of any subclass of X).

> on the instance they are associated with. If you have a predeclared
> self, only calls 2 and 3 would work, if the self parameter is just
> another parameter for the function, I can miraculously call the function
> just like it is (see call 1).

You seem to be wrongly assuming that "call 1" works, without having
tested it -- in fact, it fails in any version of Python.  Therefore,
the "fact" that it works, being not a fact, cannot serve to support
"having no predeclared self" (nor any other thesis).

As for me, I have no special issue with "having to specify self" for
functions I intend to use as bound or unbound methods; otherwise I
would no doubt have to specify what functions are meant as methods
in other ways, such as e.g.

   def [method] test(what, ever):
      ...

and since that is actually more verbose than the current:

   def test(self, what, ever):
      ...

I see absolutely no good reason to have special ad hoc rules, make
"self" a reserved word, etc, etc.  Python's choices are very simple
and work well together (for the common case of defining methods
that DO have a 'self' -- classmethod and staticmethod are currently
a bit unwieldy syntactically, but I do hope that some variation on
the often-proposed "def with modifiers" syntax, such as

    def [classmethod] test(what, ever):
        ...

or

    def test(what, ever) [classmethod]:
        ...

will make it past the BDFL filters in time for Python 2.4:-).


Alex





More information about the Python-list mailing list