[Tutor] Object references in Python
Mats Wichmann
mats at wichmann.us
Tue Jul 16 17:08:03 EDT 2019
On 7/16/19 2:33 PM, Steven D'Aprano wrote:
> x = Parrot()
>
> Now x is a reference to a Parrot instance. y remains a reference to the
> list.
>
> x.colour is a reference to the string "blue" (by default).
>
> x.speak is a reference to the "speak" method of Parrot objects.
>
>
>
> Does this help?
>
>
>
Let's add one more little cute one for good measure:
>>> def foo():
... print("This function does Foo")
...
>>> foo()
This function does Foo
>>> # we created a function object, and foo is a reference to it
...
>>> x = foo
>>> # x should be a reference to the same object
...
>>> x()
This function does Foo
>>> x is foo
True
>>> def foo():
... print("This function no longer Foos")
...
>>> # we created a new function object, and foo is now a reference to it
...
>>> foo()
This function no longer Foos
>>> x()
This function does Foo
>>> # x is still a reference to the original function object
...
>>> x is foo
False
>>>
More information about the Tutor
mailing list