This should be a simple question...

Tim Chase python.list at
Fri Mar 6 18:22:14 CET 2009

>> As Diez suggests, if you don't want to litter your global namespace, use
>> a class:
>>   class Foo:
>>     x = 1
>>     b = 2
>>     @classmethod
>>     def A(cls, *args, **kwargs):
>>       do_stuff_with(Foo.x, Foo.b, args, kwargs)
>>     @classmethod
>>     def B(cls,*args, **kwargs):
>>       do_other_stuff_with(Foo.x, Foo.b, args, kwargs)
>>   Foo.A(3, 1, 4)
>>   Foo.B("Hello", recipient="world")
> That's not what classes are really for, though ...
> If x and b are meant to be global than bite the bullet and *make* them
> global.

Assuming there's commonality between A and B (as suggested by the 
reuse), they should likely be scoped together.  This might be via 
a class (as above), or via a sub-module.  Alternatively, if the 
commonality is with the constants, but not with the functions, 
one could just use a class as a namespace container for the values:

   class Foo:
     x = 1
     b = 2

   def A(...):
     do_something(42 + Foo.x)
   def B(...):
     do_something_unrelated(3.14159 * Foo.b)

If the constants don't actually share any conceptual commonality, 
then SteveH is right, that they really should just be globals.


More information about the Python-list mailing list