Skip to content Skip to sidebar Skip to footer

Variable Scope Outside Of Classes

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. Not

Solution 1:

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).

Solution 2:

In this code:

global ftp_client # does it reference the variable of the outer scope?self.ftp_client = ftplib.FTP('foo')

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:

global ftp_client
ftp_client = ftplib.FTP('foo')

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.

classFtpFileCommand(sublime_plugin.TextCommand):
  ftp_client = Nonedefrun(self, args):
    FtpFileCommand.ftp_client = ftplib.FTP('foo')
    # login and stuff

Notice that the method doesn't use self so it might as well be a class method:

classFtpFileCommand(sublime_plugin.TextCommand):
  ftp_client = None  @classmethoddefrun(cls, args):
    cls.ftp_client = ftplib.FTP('foo')
    # login and stuff

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.

Solution 3:

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:

classFtpFile(object):
    _client = None

    @classmethoddefclient(cls):
        return cls._client

    @classmethoddefsetClient(cls, client):
        cls._client = client

classFtpFileCommand(FtpFile, sublime_plugin.TextCommand):defrun(self, args):
        client = self.client()

classFtpFileEventListener(FtpFile, sublime_plugin.EventListener):defrun(self, args):
        client = self.client()

Solution 4:

Could you add a constructor to each class then pass ftp_client as an argument?

classFtpFileCommand(sublime_plugin.TextCommand):
    ...
    def__init__(self, ftp_client):
        self.ftp_client = ftp_client
    ...

classFtpFileEventListener(sublime_plugin.EventListener):
    ...
    def__init__(self, ftp_client):
        self.ftp_client = ftp_client
    ...

Solution 5:

Yak... THANK YOU SO MUCH FOR THIS!!!

You declare ftp_client as a global variable. This means it lives at the module level (where your classes are for example).

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:

module.variable

And then within that module, I declared another global variable to call it from the main script... so example...

#Main Script main.pyimport moduleA

print(moduleA.moduleA.variable)


#ModuleA code moduleA.pyimport moduleB

classclassA():

    deffunctionA():

      global moduleA_variable
      call.something.from.moduleB.classB.functionB()
      moduleA_variable = moduleB.moduleB_variable

ModuleB code moduleB.py

classclassB():

     def functionB():

      global moduleB_variable
      moduleB_variable = retrieve.tacos()

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.

Post a Comment for "Variable Scope Outside Of Classes"