-1

I'm using Python built-in globals() in multiple functions within a class and I'd like to know whether it is good practice to save globals() to a variable first like this

available_globals = globals()

or directly using it like this:

app_name = globals()['App_Name']

In a single python script i will be using globals() more than 20 times.

Also please help me how to measure execution time taken by script.

PAR
  • 624
  • 1
  • 5
  • 16
  • 1
    The best way is to not do that at all, even once. Don't use `globals()`. It's a debugging tool. – TigerhawkT3 Nov 28 '16 at 05:14
  • I m importing a module. From imported module i want to access all the variables. Is there any specific method to access these variables. – PAR Nov 28 '16 at 05:16
  • you can time your script using the unix time utility, or see this question http://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python. The time to create a variable is minimal in python so it is really up to you weather to make multiple calls to the globals function or to create a local variable. If you have many calls you might prefer the variable method. – sakurashinken Nov 28 '16 at 05:17
  • `for att in dir(importedmodule): do_something(getattr(importedmodule, att))` – TigerhawkT3 Nov 28 '16 at 05:18
  • 2
    In a nutshell, you have an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – TigerhawkT3 Nov 28 '16 at 05:19
  • Why do you need to access all the variables in the imported module? – Metareven Nov 28 '16 at 08:26
  • I need a dictionary where it has all variables with its values including from imported modules. – PAR Nov 28 '16 at 09:14

2 Answers2

0

In python it doesn't matter. It used to matter in older versions of PHP.

I have to admit it is not a good idea to rely on global variables as far as possible. Try to pass arguments rather than directly accessing the global variables.

Consider the following example (Bad example).

num1 = 5

def add(num2):
    return num1 + num2

the add function is not reliable and unpredictable because it returns different value depending upon the state of the global variable.

Now imagine some other part of your program executes the following code

del num1

In other words the num1 variable no longer exists but the add function still relies on the num1 global variable and the add function gets broken.

Now consider the following example (Better example).

def add(num1, num2):
    return num1 + num2

There is still no type safety but it is a lot more better than the previous one and the function is more predictable and self contained.

You have to use a profiler to measure the execution speed of your scripts. Consider using a proper IDE like PyCharm.

Eddie
  • 1,043
  • 8
  • 14
0

I would say it's a good practice to avoid globals() in your code. Depending on the place of your code globals() can return different result that makes your script difficult to read. That's why I would recommend you to use something like that instead:

x_const = 3.1415926
x_vary = 16

def func_1():
    return x_const

def func_2(x_new):
    global x_vary # use 'global' if you do want to change the variable
    x_vary = x_new
Fomalhaut
  • 8,590
  • 8
  • 51
  • 95