Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Python ➜ Python globals

Python globals

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Martijn   (20 posts)  Bio
Date Tue 23 Oct 2007 12:06 PM (UTC)
Message
This doesn't work:


empties = 0

def addempty(theName, theOutput, wildcards):
  global empties += 1



It's probably a problem with globals in Python, and the way MUSHclient calls python scripts, but I can't think of a way around it, apart from using a 'Global Mushclient variable'.

It's part of a plugin, and I would like to keep it contained in the plugin, and not muck around with world variables. If I have no choise however, I have no choise, but I guessed someone may have found a way around it. If anyone could lend a hand, I would be most gratefull. If you need more information on the plugin, just ask, and I'll post the rest.
Top

Posted by Ked   Russia  (524 posts)  Bio
Date Reply #1 on Tue 23 Oct 2007 01:22 PM (UTC)
Message
Change it to:


empties = 0

def addempty(theName, theOutput, wildcards):
  global empties 
  empties += 1
Top

Posted by Martijn   (20 posts)  Bio
Date Reply #2 on Tue 23 Oct 2007 03:45 PM (UTC)
Message
yup, thanks. I always muck up my Python globals.
Top

Posted by Elin   (13 posts)  Bio
Date Reply #3 on Tue 10 Jun 2008 06:46 PM (UTC)
Message
I mess these up, too, and always forget to type 'global variable' in my script function.

I've started just writing all the code out, and then going back through and typing all the globals out when I'm done.
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #4 on Tue 10 Jun 2008 10:10 PM (UTC)
Message
For future reference, it might help to consider the global keyword as a declaration. "The variable with the name empties is considered global and will be treated as if we were in the global scope." Notice how it doesn't say anything about the current value or the new values it may get in the function. :)
Top

Posted by First Lensman   (2 posts)  Bio
Date Reply #5 on Tue 03 Feb 2009 01:26 PM (UTC)
Message
NOTE: I posted this in another topic regarding Global Variables

-----
It was recommended in a python manual to create a class 
that holds global variables. Here is an example that I 
culled from a script I wrote to work with the mailman 
software package:

class GlobalVariables:
    'Class that holds Global Variables'
    def __init__(self):
        self.LDIR = '/var/lib/mailman/lists'
        self.BDIR = '/usr/lib/mailman/bin'
        self.THLD = ''
        self.MLST = ''
        self.EOJ = 0


Then, in the main(), create a varaible using this class:

def main():
    global GL
    GL = GlobalVariables()
    input_list()
    process_list()


In any other subroutine that would need access to the 
Global Variables, just include the global statement. The 
following code is a very simplified version from my script:

def input_list():
    global GL
    GL.MLST = raw_input("Enter Mailing List: ")

def process_list():
    global GL
    cfgfile = '/tmp/'+GL.MLST+'_config'
    regfile = '/tmp/'+GL.MLST+'_member'
    digfile = '/tmp/'+GL.MLST+'_digest'


As you can see, it is much easier to remember the "global GL" 
in each routine rather than multiple "global" statements!

Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


20,835 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.