A question on modification of a list via a function invocation

Ian Kelly ian.g.kelly at gmail.com
Wed Aug 16 19:29:17 EDT 2017


On Wed, Aug 16, 2017 at 5:03 PM, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> Chris Angelico <rosuav at gmail.com> writes:
>>objects exist independently of names
>
>   When the object »Example()« loses its name below,
>   it loses its existence.
>
> class Example(object):
> ...     def __init__(self):
> ...         print( 'initialized' )
> ...     def __del__(self):
> ...         print( 'deleted' )
> ...
>>>> a = Example()
> initialized
>>>> a = 2
> deleted

Try:

>>> class Example(object):
...   def __init__(self):
...     print('initialized')
...   def __del__(self):
...     print('deleted')
...
>>> a = Example()
initialized
>>> b = [a]
>>> a = 2
>>>

The object no longer has a name, but it still exists.



More information about the Python-list mailing list