-2
$\begingroup$

I need that certain value of a variable be available in other functions.

Furthermore I want it initialised when loading the game, at which point the variables are set to their default value. Within functions, the value of the variables may be changed, but the value, which was initially assigned to the variable, is not affected by the new value which the function may assign the variable.

I want the initial value to be constant and for it to be impossible to update the value.

I'm grateful for your help, I'm a beginner in Python.

$\endgroup$
1
  • $\begingroup$ Your question in its original form is somewhat confusing. I have attempted to clarify what you are asking. If I am incorrect in my assumptions, and edit, then please do not hesitate to roll back the edit. $\endgroup$ Dec 4, 2016 at 1:04

1 Answer 1

0
$\begingroup$

If I understand your question correctly, then I think you mean that you want a global constant that is initialised at the start of the game. The value of this global constant should be accessible in other functions, but those functions should not be able to change the value of that constant. Is that correct?

If I am correct in my assumption above, then the answer is in this question on StackOverflow: How do you declare a global constant in Python?:

You can just declare a variable on the module level and use it in the module as a global variable. An you can also import it to other modules.

#mymodule.py
GLOBAL_VAR = 'Magic String' #or matrix...

def myfunc():
    global GLOBAL_VAR
    #do something

Or in other modules:

from mymodule import GLOBAL_VAR
$\endgroup$
2
  • $\begingroup$ ok thank you this solved my question. $\endgroup$
    – cod1ng
    Feb 1, 2020 at 14:30
  • $\begingroup$ If this solved your problem, then please mark this question as the accepted answer, and upvote. Thanks. $\endgroup$ Jul 31, 2020 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.