My text editor of choice is extensible through python plugins. It requires me to extend classes and override its methods. The general structure looks similar the snippet below. Note that the function signature is fixed.
ftp_client
is supposed to be shared by instances of both classes.
ftp_client = None
class FtpFileCommand(sublime_plugin.TextCommand):
def run(self, args):
global ftp_client # does it reference the variable of the outer scope?
self.ftp_client = ftplib.FTP('foo')
# login and stuff
class FtpFileEventListener(sublime_plugin.EventListener):
def run(self, args):
global ftp_client # same for this
self.ftp_client.quit() #
Both of these classes are supposed to have one variable in common. What is the best practice in order to share variables?
Edit based on madjars answer:
FtpFileCommand.run
is called first, instanciates ftp_client
and works like a charm. FtpFileEventListener.run
is called later and, can reference ftp_client
perfectly but it is still None
. Using the global keyword, does it add the variable as a member to self
?
Yak... THANK YOU SO MUCH FOR THIS!!!
I was having a difficult time trying to write my program "properly" where I'm utilizing classes and functions and couldn't call any of the variables. I recognized that global would make it available outside of the class. When I read that I thought... If it lives outside of the class then the variable I need to retrieve from the py script that I'm importing that module from would be:
And then within that module, I declared another global variable to call it from the main script... so example...
ModuleB code moduleB.py
I hope my explanation also helps someone. I'm a beginner with python and struggled with this for a while. In case it wasn't clear... I had separate custom modules made up of a few different .py files. Main was calling moduleA and moduleA was calling moduleB. I had to return the variable up the chain to the main script. The point of me doing it this way, was to keep the main script clean for the most part, and set myself up for executing repetitive tasks without having to write pages of crap. Basically trying to reuse functions instead of writing a book.
If there's only a single shared variable, then a global is the simplest solution. But note that a variable only needs to be declared with
global
when it is being assigned to. If the global variable is an object, you can call its methods, modify its attributes, etc without declaring it as global first.An alternative to using global variables is to use class attributes which are accessed using classmethods. For example:
In this code:
you declare
ftp_client
as a global variable. This means it lives at the module level (where your classes are for example).The second line is wrong. You wanted to assign to the global variable but instead you set an instance attribute of the same name.
It should be:
But let me suggest a different approach. A common practice is to put such stuff inside the class, since it is shared by all instances of this class.
Notice that the method doesn't use
self
so it might as well be a class method:This way you will get the class as the first argument and you can use it to access the FTP client without using the class name.
Yep, that's exactly how
global
works.It seems to me you are doing it right, as it's done this way in some modules of the python standard library (fileinput, for example).
Could you add a constructor to each class then pass
ftp_client
as an argument?