Janis Lesinskis' Blog

Assorted ramblings

  • All entries
  • About me
  • Projects
  • Economics
  • Misc
  • Software-engineering
  • Sports

Python self references


This is a bit of Python trivia but Python containers can store items that are self references.

The way this works is that in Python a list just stores references to Python objects so you can just have a reference to the container. You can create such a list like this:

>>> foo = ['bar', 'baz']
>>> foo.append(foo)
>>> foo
['bar', 'baz', [...]]
>>> foo[2] is foo
True

So you can see here from the object identity test that foo has a self reference in it now.

This knowledge can be useful sometimes (like explaining to people why there's 2 garbage collection mechanisms in CPython) but for the most part this will just be a bit of trivia.

Published: Sun 22 April 2018
By Janis Lesinskis
In Software-engineering
Tags: Python trivia indirection

links

  • JaggedVerge

social

  • My GitHub page
  • LinkedIn

Proudly powered by Pelican, which takes great advantage of Python.