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

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Python
. . -> [Subject]  Global variables

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Global variables
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by First Lensman   (2 posts)  [Biography] bio
Date Tue 03 Feb 2009 01:05 PM (UTC)  quote  ]

Amended on Tue 03 Feb 2009 01:18 PM (UTC) by First Lensman

Message
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

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Sat 10 May 2003 09:36 PM (UTC)  quote  ]
Message
Re the disconnection, I am glad the variable is preserved, I was guessing that it did not, and thought that might be Poremenos' problem. In my post I said "This probably means it reinitialises variables.".

Seems it is smarter than that, and my word "probably" was covering the fact that I hadn't actually tested it. :)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Sat 10 May 2003 01:09 PM (UTC)  quote  ]
Message
Quote:

In what way is your question not answered? Can you give an example?

No, it's answered, i was referring to ixokai :)

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Ixokai   USA  (24 posts)  [Biography] bio
Date Sat 10 May 2003 08:25 AM (UTC)  quote  ]
Message
Nick, I can't seem to reproduce the behavior you described, with the script engine being 'disconnected' on a syntax error...

I mean...
[code]/a = 42
/if blah there
(gives syntax error)
/world.note(a)
(gives 42)
[/code]

What are you doing that produces the disconnection?
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Sat 10 May 2003 04:22 AM (UTC)  quote  ]
Message
In what way is your question not answered? Can you give an example?

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Sat 10 May 2003 02:35 AM (UTC)  quote  ]

Amended on Sat 10 May 2003 02:36 AM (UTC) by Poromenos

Message
Quote:
Is there any way I can set global variables that I can change, in Python?

:p

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Ixokai   USA  (24 posts)  [Biography] bio
Date Fri 09 May 2003 11:11 PM (UTC)  quote  ]
Message
Not *pecisely*. In Python you only need the "global" if you are going to assign to the global variable... to read from it, it traverses the scope-hierarchy and discovers the global all by itself..
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Tue 06 May 2003 09:18 PM (UTC)  quote  ]
Message
Ah, strangely, PHP behaves the same way, as I should know, as I do these web pages in it.

Any references to variables in a subroutine are assumed local unless it is told otherwise. I didn't know enough about Python to know that, and my tests were of variables in global scope.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Tue 06 May 2003 08:11 PM (UTC)  quote  ]
Message
intSomeInt = 3

def MyFunc:
intSomeInt = 2

Boom, error...
The secret is:

def MyFunc:
global intSomeInt
intSomeInt = 2

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Tue 06 May 2003 08:11 AM (UTC)  quote  ]
Message
Quote:

For example, if you do 'Send to Script', I notice that the command that is entered in the send area is interpreted as script-code.. I think it might be executed in the global scope


Yes, that is what it is intended to do, and yes, it executes in global scope.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Tue 06 May 2003 05:35 AM (UTC)  quote  ]
Message
My tests also indicate that a script function which sets a variable of global scope will set a variable which persists until next time (eg. an alias that sets a variable in a script, and another that tests it).

I am talking Python variables here, not MUSHclient variables.

HOWEVER, there was a problem with syntax errors. I discovered that when Python gave a syntax error it marked the script engine as "disconnected", and that further attempts to script silently failed.

Thus I recently changed it (for Python's benefit really) that if the script engine was in a disconnected state when you went to execute a script it reconnected it. This probably means it reinitialises variables.

However the exact same logic is there for the other script engines.

Thus what will happen, and is probably happening to you, is:

1. You set a variable: /a = 42
2. You make a typing error, and get syntax error
3. You get it right, but the variable has gone: /world.note (a)

Basically this is a development problem. It should go away once the scripts are debugged.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Ixokai   USA  (24 posts)  [Biography] bio
Date Tue 06 May 2003 03:34 AM (UTC)  quote  ]
Message
You just have to set them outside of the function... For example, if you do 'Send to Script', I notice that the command that is entered int he send area is interpreted as script-code.. I think it might be executed in the global scope, before any script function is called... YOu can access any variables there in the function, but they may be global and therefore remain... Test it out.

I /do/ know that this works:

/somevar = "Er, hi!"
/world.note(somevar)

So 'somevar' is indeed being set in the global scope...
[Go to top] top

Posted by Poromenos   Greece  (1,037 posts)  [Biography] bio
Date Mon 05 May 2003 06:09 PM (UTC)  quote  ]
Message
Is there any way I can set global variables that I can change, in Python? I hate it that they disappear every time my function exits :/

Vidi, Vici, Veni.
http://porocrom.poromenos.org/ Read it!
[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.


43,218 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]