python - What's the use of a circular reference? -


in python can append list , accept assignment.

>>> l = [0,1] >>> l.append(l) >>> l [0, 1, [...]] >>> l[-1] [0, 1, [...]] 

my question why?

python allows rather throwing error, because there's potential use or because wasn't seen necessary explicitly forbid behaviour?

is because there's potential use or because wasn't seen necessary explicitly forbid behaviour?

both. lists store references, , there no reason prevent them storing otherwise-valid references.

as potential uses, consider generic top-down-shooter type video game:

  • a level contains reference each enemy, can draw , update them each frame.
  • an enemy contains reference level, can (for example) query distance player or spawn bullet in level.

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -