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

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

ruby - Net::HTTP extremely slow responses for HTTPS requests -