I've worked a lot with Python in Jupiter Notebook. In the beginning my projects were small, and everything worked fined. However, my projects are becoming bigger, and my notebooks can become messy with dozens of functions and hundreds of programming lines. Therefore I would like to structure my code better.
The code below is an example of what a project might look like. I am using a global variable, which other functions might change or add unto.
import numpy as np
def init():
global my_list
my_list=[]
return
def add_value(a):
my_list.append(a)
return
def add_array(a,b):
array=np.arange(a,b)
my_list.append(array)
return
init()
add_value(3)
add_array(6,9)
This way of coding offers the advantage of putting all variables and functions in the first cells, and working with 'clean' code in the space below. Since many functions might be present, the notebook can become very large, and messy. Therefore I've tried to store the functions in Python files, and import them in the Notebook. However, the files do not recognize the global variables, even if I put 'global my_list' in the beginning.
So my first question is: is there a way to structure my code like this, using global variables? And my second question: is there a better alternative to how I've structured it? If so, please provide a snippet of code.