Python:在“实例方法”的第一个参数中使用__self__而不是self

huangapple go评论65阅读模式
英文:

Python: Using __self__ instead of self as first argument of "instance method"

问题

The __self__ parameter in this code appears to be a convention used for indicating that this method is intended to be a bound method, which means it can be called on an instance of the class. However, it's not a standard Python convention, and its use in this code might be unconventional or unnecessary. It doesn't have any special built-in behavior in Python.

In Python, instance methods typically have the self parameter as their first argument, which represents the instance itself. Using __self__ instead of self is not a common practice, and it might lead to confusion for other developers reading the code.

So, in summary, while the code you provided is valid Python, the use of __self__ as the first parameter is not a standard Python convention, and it might not serve any specific purpose other than potentially confusing readers.

英文:

I recently came across this piece of code:

class InlineParameterResolver:
    def resolve(__self__, value: str) -> Any:
        return value

Now, I am aware that this code is strange as the class itself has no instance methods, nor an init method to enable it to create an instance and operate on its internal attributes, but does having the __self__ as the first argument have any special function/behaviour?

I have tried to google for an answer, but couldn't find anything, I just want to know if this is an opportunity to learn something, or if I'm just reading Python from someone who didn't have a lot of experience with the language.

Cheers.

答案1

得分: 4

Having the __self__ as the first argument does not have any special function or behavior. It's simply a naming convention for the first positional argument, and you can use other names like foo or anything else with the same effect. However, using __self__ as a name is not advisable because it has a different meaning in the datamodel. In this context, __self__ refers to the instance when an instance method object is created, and the method object is considered bound. Using double underscore names as function arguments can be confusing, so it's recommended to avoid it.

英文:

> does having the __self__ as the first argument have any special function/behaviour?

There is no special function or behavior. Even using self is just a naming convention for the first positional argument, using foo or anything else would work all the same.

However, using the name __self__ is not advisable, because that already has a different meaning in the datamodel: when an instance method object is created by retrieving a function object from a class via one of its instances, its __self__ attribute is the instance, and the method object is said to be bound.

>>> class A:
...     def f(self):
...         pass
... 
>>> a = A()
>>> a.f.__self__
<__main__.A object at 0xdeadbeef>

Using double underscore names as function arguments is strange enough to cause confusion, so I'd recommend avoiding that.

答案2

得分: 1

以下是您要翻译的内容:

你描述了一些来源不明的代码:

    def resolve(__self__, value):

其中任何一个都会表现相同:

    def resolve(foo, value):

    def resolve(baz, value):

    def resolve(self, value):

建议选择最后一个,因为它是常规做法。

我们编写代码是为了与机器交流,是的。
但是主要我们编写代码是为了清晰地传达作者的意图
给读者。
在Python上下文中,不使用self(或类方法中的cls
会使意图变得模糊,让亲切的读者困惑不解。
走常规路线。

英文:

You described some code of uncertain provenance:

    def resolve(__self__, value):

Any of these would behave identically:

    def resolve(foo, value):

    def resolve(baz, value):

    def resolve(self, value):

Prefer the last one, as it is conventional.

We write code to communicate with the machine, yes.
But primarily we write so as to clearly communicate Author's Intent
to the reader.
In a python context, not using self (or cls for a classmethod)
is going to obscure the intent and leave the Gentle Reader scratching his head
in confusion. Go the conventional route.

huangapple
  • 本文由 发表于 2023年5月15日 09:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定