0

Is there any problem, if in a periodic script I have a "constant" variable and I redeclare it in every run?

#!/usr/bin/env python
PATH_PATTERN = '/home/%s/config.xml'
PATH = None

def periodic_execution(function):
    PATH = PATH_PATTERN % get_user()

    interval_in_sec = 1000
    threading.Timer(interval_in_sec,periodic_execution,[function]).start()
    function()
    # in function i use the PATH variable

def main():

    periodic_execution(tasks)

if __name__ == '__main__':
    main()

I know that constant not will be constant...

tmsblgh
  • 517
  • 5
  • 21

2 Answers2

3

Beware! In shown code, the PATH variable is a global one, but in periodic_execution you assign to PATH before using it and without declaring it as global => Python actually creates a local PATH variable in the function and leaves the global untouched which is certainly not what you expect.

You should write:

def periodic_execution(function):
    global PATH
    PATH = PATH_PATTERN % get_user()

to change a global variable.

But this is by no way a constant. AFAIK you cannot declare true constants in Python, even if you can build read only properties.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

Did you also look at the keyword global in Python? See this link. It is necessary since you change the variable in a function.

Community
  • 1
  • 1
EmbedWise
  • 184
  • 1
  • 12