1

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.

Mark
  • 45
  • 5
  • 1
    If you care about clean code, don't use global variables. – matszwecja Mar 15 '23 at 17:33
  • 2
    `global` variables are widely considered bad practice for many reasons. One being that as your program grows, they become harder to track (even more so if you've hidden them in a function or across many functions). At a certain point, you will no longer be able to fit all the possible states of your program in your head. To solve this, typically, you would create small functions which take some input and `return` a calculation on that input. This gives you the benefit of only having to focus on a small piece of your program at a time. – Axe319 Mar 15 '23 at 17:33
  • 4
    "A better way" is often entirely opinion-based, however I would encourage you to look at [Why are global variables evil?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) for some interesting perspective. In practice, I have never used a global variable in my code – G. Anderson Mar 15 '23 at 17:34
  • 3
    As an easy to see example why they are bad, try reusing your code for some other list, while keeping `my_list` intact - you can't, because your code only ever works with this single variable named `my_list`. – matszwecja Mar 15 '23 at 17:36
  • Thank you all for your time. However, none of these solve my issue. 'Don't use global variables' is not something that brings me further, but rather slows me down. Thank you again for the comments. – Mark Mar 17 '23 at 16:39
  • After some research, I have found the answer here: https://stackoverflow.com/questions/13034496/using-global-variables-between-files – Mark Mar 17 '23 at 17:16

0 Answers0