By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Global Variables

P: n/a
If I have a module File
which has some fucontions but I need globals filename and path how can I
set them so I can change them because I tryed.

filename="log.txt"
path="/home/Bob/"

def change_filename():
filename=raw_input()

def change_path():
path=raw_input()

they don't change and without the declarations there not global.

Help Please
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Use 'global':

def change_filename():
global filename
filename=raw_input()
def change_path():
global path
path=raw_input()
Even better, don't use globals at all; in 99% if the time, there are
better ways to achieve the same effect.

George

Jul 18 '05 #2

P: n/a
"Bob Then" <Bo******@yahoo.com.au> wrote:
If I have a module File
which has some fucontions but I need globals filename and path how can I
set them so I can change them because I tryed.

filename="log.txt"
path="/home/Bob/"

def change_filename(): global filename filename=raw_input()

def change_path(): global path path=raw_input()

they don't change and without the declarations there not global.


However, the reason that must be stated explicitly is because it's a bad
practice. It means that your function has "side effects" beyond just
returning a value or set of values. This kind of thing is a better
solution:

def change_filename():
return raw_input()

def change_path()
return raw_input()

filename = change_filename()
path = change_path()
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.