5

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?

1

2 Answers 2

9

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).

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.