Im new to python , trying to update my self from the time of ASP Classic.
In that time i use to have a page for vars that i included in my main page.

I like that method because i can keep all my vars organized.

I tried to do that with python but allways get errors, yes i tried declare them as global as well but still got errors.

Is there a way to do that with python or is it better to declare them as you go?

thanks

Also, can i put my vars in a txt like this:

var1=something1
var2=something2

and access them from python?

can i put my vars in a txt like this:

var1=something1
var2=something2

and access them from python?

Would it be acceptable to use a Python file with these as globals, and then import the data that way? It would leverage the Python package system to avoid having to explicitly read the data files. I don't know if this is something which would be feasible for your project, but it might be worth looking into.

commented: the idea is to add remove change the content of the software without using the code, just fill the txt and your set for your needs +1

Overall, in Python (and nearly every other language) you generally want to use global variables as little as possible, and share variables between functions and methods by passing them as arguments. You can use globals, but you would be fighting the language's overall procedural/OOP design in doing so.

As a rule, you want to limit the scope of variables to the narrowest visbility that is usable, not the widest. Otherwise, you could end up with undesirable side effects where a change in a variable in one place causes its use elsewhere to be compromised. Ideally, when working on one function you don't need to pay any attention to the local variables in any other function - that's why they are local, you can't access them from anywhere outside of the local scope.

Yes, you can do that. The normal way to do that is to create a source file with the variable definitions that you then import into other source files that want to use them. A simple example:

# This is "my_app_globals.py"
MY_APP_NAME    = "My Application"
MY_APP_VERSION = "1.02"
MY_APP_NVERSION = (1, 2)

Then you can ues that file in another Python source file with something like:

# This is "main.py"
import my_app_globals

print("App Name:", my_app_globals.MY_APP_NAME)
print("Version: ", my_app_globals.MY_APP_VERSION)

The longer story is that import reads and compiles all of a single Python source file and puts of of the definitions into a module with a name that is (by default) the source file name without the .py suffix. (The compilation part will be skipped if an up-to-date precompiled file is found, but that's a run-time optimization. The principle is the same.) Any top-level code (not in a class or function definition) will be run once at import time to "initialize the module". All global names in that source file are stored in the module, available to other modules import it.

Those names are pretty long. You can shorten them in a couple of ways. One way that's pretty safe is to simply rename the module on import:

import my_app_globals as gbls
print("App Name:", gbls.MY_APP_NAME)

That makes "gbls" a name your program can use to access the "my_app_globals" module. The original module is only imported once per execution of your program. Another way is to use another form of the import command to copy names from the imported module into your module's global namespace. I do not recommend this. It hides the fact that the value was created in another module, and also can create subtle (meaning "hard-to-find") bugs when importing progams modify the imported variables. It looks like this, when used:

from my_app_globals import * # CAUTION: Avoid unless absolutedly needed
print(MY_APP_NAME)    

There's more to learn about Python modules and importing them, but this might get your current work moving along for now. "Python modules" is a topic you should look up and/or review in whatever you've been using to learn the Python language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.