[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Python
. . -> [Subject]  Python globals

Python globals

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


Posted by Martijn   (20 posts)  [Biography] 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.
[Go to top] top

Posted by Ked   Russia  (524 posts)  [Biography] 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
[Go to top] top

Posted by Martijn   (20 posts)  [Biography] bio
Date Reply #2 on Tue 23 Oct 2007 03:45 PM (UTC)
Message
yup, thanks. I always muck up my Python globals.
[Go to top] top

Posted by Elin   (13 posts)  [Biography] 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.
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] 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. :)
[Go to top] top

Posted by First Lensman   (2 posts)  [Biography] 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!

[Go to top] 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.


18,706 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]