469,280 Members | 1,810 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,280 developers. It's quick & easy.

Global Variables

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
2 1555
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
"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.

Similar topics

10 posts views Thread by Matt | last post: by
4 posts views Thread by Andrew V. Romero | last post: by
12 posts views Thread by David WOO | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
5 posts views Thread by Sandman | last post: by
112 posts views Thread by istillshine | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.