You can create a namespace object β an object that functions as a namespace to preserve the global namespace:
class namespace(): pass global_data=namespace() def add_attributes(obj): obj.foo="bar" add_attributes(global_data) print (global_data.foo)
However, this is only slightly better than using the global keyword. You really want a class here, as Paul mentioned.
source share