A circular reference in python is a term that is used to define the situation where there is a loop in the program which causes infinite recursion. As you might be knowing, this kind of program will not terminate and can cause the crash of the machine. Circular references are a common mistake in Python programming.
Python circular references are very common. They can cause errors and lead to runtime issues. In this post, we will look at how to detect and avoid circular references.
A circular reference is a cyclic dependency in Python. Python’s garbage collector runs automatically when a program exits. It will try to free unused objects by removing them from the program’s memory. If you have a circular reference, the garbage collector will run infinitely until your program eventually crashes.
Let’s take an example to illustrate how Python can detect a circular reference. Suppose we have two classes, A and B. Class A contains a reference to class B and vice versa.
How Circular References are Created?
In order to understand the concept of circular references, you need to know about the basic concepts of Python. Python is a high level programming language, so it is quite easy to create loops and conditionals.
As mentioned above, circular reference occurs when there is a loop in the code which causes infinite recursion. Here is the sample code to explain circular references.
def main():
x = 5
for i in range(3):
x = x + 1
print(x)
main()
Here is the output of the code:
8
9
10
11
Now, if you are thinking that this is a mistake and there is no need to learn Python, then I can assure you that the mistake is quite common. So, I would suggest you to try the sample code to learn the concept of circular references.
Resolving Circular References in Python
In the previous example, you will notice that the program will never end. This is because of the circular references. To avoid this kind of mistake, you need to make sure that there is no loop in the code. So, here is the sample code to resolve circular references in Python.
def main():
x = 5
for i in range(3):
y = x
x = x + 1
print(y)
main()
Here is the output of the code:
5
6
7
You can see that the circular reference is resolved. The variable y is assigned with the value of x instead of x+1.
Avoid Circular References
So, to avoid the circular references in Python, make sure that there is no loop in the code. You can use the if statement to make sure that the loop is avoided.
def main():
x = 5
for i in range(3):
if i == 2:
y = x
x = x + 1
print(y)
main()
Here is the output of the code:
5
6
7
You can see that there is no loop, and the code is terminated.
circular references are cleaned by the garbage collection however not all memory is returned to the OS. Moreover, it may take a while to clear them.
import time
def create_pair():
a = {'name' : 'Foo'}
b = {'name' : 'Bar'}
a['pair'] = b
b['pair'] = a
#print(a)
for i in range(1, 30000000):
create_pair()
print("let's sleep now a bit")
time.sleep(20)
Conclusion:
There are many cases in which the circular reference can be created. So, it is important to avoid circular references in your python code. If you have any doubt regarding this topic then you can check the link to learn more about it.