alex wrote:
[color=blue]
> Hi,
>
> is it possible to create 'global' variables that can be seen in all
> other classes?
>
> Alex
>[/color]
Not sensibly, though you can mess around with the __builtin__ namespace
to make values accessible without qualification.
The usual solution is to maintain a config module that establishes
default settings for configuration variables. Other modules that import
config can access (and change) those values using
alex wrote:[color=blue]
> Hi,
>
> is it possible to create 'global' variables that can be seen in all
> other classes?
>
> Alex[/color]
Hello,
What about using a class?
Py> class globalVar:
.... pass
Py> globals = globalVar()
Now you can assign 'variables' to it.
And use it anywhere you need it.
M.E.Farmer wrote:[color=blue]
> alex wrote:[color=green]
>> is it possible to create 'global' variables that can be seen in all
>> other classes?[/color]
>
> What about using a class?
>
> Py> class globalVar:
> ... pass
>
> Py> globals = globalVar()[/color]
Probably naming it something other than 'globals' would be a good idea
-- otherwise you'll hide the builtin globals() function.
But I agree that the attributes of a class instance (as you suggest) or
the attributes of a module (as Steve Holden suggests) is probably the
right way to go.
class a:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.arg1=arg1
self.arg2=arg2
self.__dict__.u pdate(kwargs)
return
class b:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.__dict__.u pdate(kwargs)
self.a=a(arg1, arg2, **kwargs)
return
class c:
def __init__(self, arg1, arg2, **kwargs):
#
# Dictionary kwargs will have keyword, value pairs
# that can be used as global space.
#
self.__dict__.u pdate(kwargs)
self.b=b(arg1, arg2, **kwargs)
return
you will have global1, global2, global3, and global4 attributs
in all classes. If you don't want the attributes, just access
to the values, delete the self.__dict__.u pdate(kwargs) lines.
Larry Bates
alex wrote:[color=blue]
> Hi,
>
> is it possible to create 'global' variables that can be seen in all
> other classes?
>
> Alex
>[/color]
Steve,
Yes I agree ;) Never use builtin names.
I know better but missed it somehow.
I apologize for any confusion I may have caused.
Thank you Steve for the correction.
M.E.Farmer
Steven Bethard wrote:[color=blue]
> M.E.Farmer wrote:[color=green]
> > alex wrote:[color=darkred]
> >> is it possible to create 'global' variables that can be seen in[/color][/color][/color]
all[color=blue][color=green][color=darkred]
> >> other classes?[/color]
> >
> > What about using a class?
> >
> > Py> class globalVar:
> > ... pass
> >
> > Py> globals = globalVar()[/color]
>
> Probably naming it something other than 'globals' would be a good[/color]
idea[color=blue]
> -- otherwise you'll hide the builtin globals() function.
>
> But I agree that the attributes of a class instance (as you suggest)[/color]
or[color=blue]
> the attributes of a module (as Steve Holden suggests) is probably the
> right way to go.
>
> Steve[/color]
On Fri, 04 Feb 2005 23:56:44 +1000, rumours say that Steve Coghlan
<scoghlan@iinet .net.au> might have written:
[color=blue]
>A Steve wrote:[color=green]
>> A Steve wrote:
>>[color=darkred]
>>> A Steve wrote:
>>>[/color][/color]
>
>There we go, much clearer ;)[/color]
Indeed. I recall some Dan Perl who was advised to change his name to a more
pythonic one, but now I see he misinterpreted the advice.
Am I assimilated or what?
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Comment