In Python you can append a list to itself and it will accept the assignment.
>>> l = [0,1]
>>> l.append(l)
>>> l
[0, 1, [...]]
>>> l[-1]
[0, 1, [...]]
My question is why?
Python allows this rather than throwing an error, is that because there's a potential use for it or is it just because it wasn't seen as necessary to explicitly forbid this behaviour?
At times, you may want to use circular references because they cause your functions to iterate—repeat until a specific numeric condition is met. This can slow your computer down, so iterative calculations are usually turned off in Excel.
Circular references are not always harmful - there are some use cases where they can be quite useful. Doubly-linked lists, graph models, and computer language grammars come to mind. However, as a general practice, there are several reasons why you may want to avoid circular references between objects.
A circular reference is a series of references where the last object references the first, resulting in a closed loop.
%) error message explained. Millions of people using Excel don't get why they see the “circular reference” error message right after they've entered a formula. The message means that your formula is trying to calculate its own cell–kind of like when a dog chases its own tail.
is that because there's a potential use for it or is it just because it wasn't seen as necessary to explicitly forbid this behaviour?
Both. Lists store references, and there is no reason to prevent them from storing certain otherwise-valid references.
As for potential uses, consider a generic top-down-shooter type video game:
Level
contains a reference to each Enemy
, so that it can draw and update them each frame.Enemy
contains a reference to its Level
, so that it can (for example) query the distance to the Player
or spawn a Bullet
in the Level
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With