In Python, does 'return self' return a copy of the object or a pointer?

后端 未结 2 1389
太阳男子
太阳男子 2021-02-19 21:05

Let\'s say I have a class

class A:
    def method(self):
        return self

If method is called, is a pointer to the A

2条回答
  •  Happy的楠姐
    2021-02-19 21:38

    It returns a reference:

    >>> a = A()
    >>> id(a)
    40190600L
    >>> id(a.method())
    40190600L
    >>> a is a.method()
    True
    

    You can think of it this way: You actually pass self to the .method() function as an argument and it returns the same self.

提交回复
热议问题