Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of a circular reference?

Tags:

python

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?

like image 751
SuperBiasedMan Avatar asked Apr 29 '15 13:04

SuperBiasedMan


People also ask

What is the use of circular reference?

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.

Is it okay to have circular references?

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.

What is meant by circular reference?

A circular reference is a series of references where the last object references the first, resulting in a closed loop.

Why do we use circular reference error?

%) 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.


1 Answers

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:

  • A Level contains a reference to each Enemy, so that it can draw and update them each frame.
  • An 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.
like image 152
Score_Under Avatar answered Sep 20 '22 07:09

Score_Under