Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing self to class functions in Python [duplicate]

What's the reason of passing a value for a self reference in class functions in python? For instance:

class MyClass:
    """A simple example class"""
    i = 12345
    def f(**self**):
        return 'hello world'

By doing this, aren't you doing the compiler's work?

like image 644
Joan Venge Avatar asked Feb 03 '09 23:02

Joan Venge


2 Answers

Many electrons have given their lives to discussing this question over the years.

Guido (python's creator) weighs forth on the issue in his blog here, in response to a proposal last year to get rid of the explicit self. The python FAQ also covers the issue.

Finally, if you don't mind a bit of grey magic, you can use a metaclass to get rid of it. Although there are good reasons why you shouldn't do that (it breaks properties, will make it harder for you to understand other people's code, and it will confuse any other python programmer who looks at your code).

like image 114
John Fouhy Avatar answered Sep 22 '22 16:09

John Fouhy


Guido says: http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay.html

like image 24
Hank Gay Avatar answered Sep 22 '22 16:09

Hank Gay