Newbie problem: global variables

John Roth johnroth at ameritech.net
Tue Apr 16 21:32:56 EDT 2002


"Mirko Opitz" <opm at hrz.tu-chemnitz.de> wrote in message
news:a9i1uk$1is$1 at narses.hrz.tu-chemnitz.de...
> Hi,
>
> I've a small problem I need a variable which is present in all
subfunctions
> an also in the main prog but I don't want do give it in the header of
all
> the functions. Is there anyway to declaire it as Public or global like
in
> other languages?
>
> Thx for now
>
> Mirko

In general, no. If you simply want to use it, then assigning
to it once at the module level is sufficient. If you want to
assign to it in various functions, you have to tell Python
that it isn't local, by using a global statement in the
function.

However, you can sidestep the issue by defining
an accessor function. That is, you can define
a function whose sole purpose is to set the variable.
That can be accessed globally, so you don't have to
declare the variable in each function where you want
to modify it.

John Roth
>
>





More information about the Python-list mailing list