Global Variables

Tim Roberts timr at probo.com
Thu Apr 14 01:38:57 EDT 2005


"Bob Then" <Bob_then at 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, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list