Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Let's say I have a class

class A:
    def method(self):
        return self

If method is called, is a pointer to the A-object going to be returned, or a copy of the object?

like image 721
Sahand Avatar asked May 19 '16 10:05

Sahand


1 Answers

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.

like image 174
Selcuk Avatar answered Oct 21 '22 01:10

Selcuk