Why self?

Christian Tanzer tanzer at swing.co.at
Fri Jul 5 11:51:33 EDT 2002


Roy Smith <roy at panix.com> wrote:

> Other than in an obfuscated python contest, I can't think of any reason
> why anybody would *ever* use any variable name other than "self" as the
> first formal parameter of a method.
>
> Have you ever seen an example that does?

Here's an example that should use another name than `self`:

    >>> class Test(object):
    ...   def __new__(self):
    ...     print "__new__", self
    ...     return object.__new__(self)
    ...   def __init__(self):
    ...     print "__init__", self
    ...
    >>> t = Test()
    __new__ <class '__main__.Test'>
    __init__ <__main__.Test object at 0x8153734>

IMHO, the first parameter of `__new__` should be called `cls`.

Similarly, for normal methods of a metaclass I'd call the first
parameter `cls` and the first parameter of `__new__` `meta`...

-- 
Christian Tanzer                                         tanzer at swing.co.at
Glasauergasse 32                                       Tel: +43 1 876 62 36
A-1130 Vienna, Austria                                 Fax: +43 1 877 66 92






More information about the Python-list mailing list