Global Variables

B

Bob Then

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
 
G

George Sakkis

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
 
T

Tim Roberts

Bob Then said:
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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,179
Members
47,811
Latest member
GregoryHal

Latest Threads

Top