I’m trying to declare a global variable from within a class like so:
class myclass:
global myvar = 'something'
I need it to be accessed outside the class, but I don’t want to have to declare it outside the class file. My question is, is this possible? If so, what is the syntax?
In your question, you specify "outside the main file". If you didn’t mean "outside the class", then this will work to define a module-level variable:
Then you can do, assuming the class and variable definitions are in a module called
mymodule:Also, in response to your comment on @phihag’s answer, you can access myvar unqualified like so:
If you want to just access it shorthand from another file while still defining it in the class:
then, in the file where you need to access it, assign a reference in the local namespace: