Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7087291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:39:22+00:00 2026-05-28T07:39:22+00:00

I’m using the foo module which includes a few module level variables that dictate

  • 0

I’m using the foo module which includes a few module level variables that dictate its behavior. One of these variables is SANITIZE.

I want to be able to use foo in two ways, foo.parse() and foo2.parse(), where the only difference is foo has SANITIZE=false and foo2 has SANITIZE=true

I want to do this while not having to copy paste my code. For example

#foo module
SANITIZE='foo'
def parse():
    print SANITIZE

#foo2 module
import foo
foo.SANITIZE='foo2'


#test script
import foo
import foo2
foo2.foo.parse() #i want this to print 'foo2'
foo.parse() #i want this to print 'foo'

However, the above example will print ‘foo2’ both times. Is there a way to achieve this behavior?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:39:23+00:00Added an answer on May 28, 2026 at 7:39 am

    If this is your code, than the solution is not to depend in module level variables, but in some other way to keep state of the objects. Modules in Python are “singletons” – which means that once imported by any module, there is just one version of then, interpreter wide –
    The behavior you want, for example, is exactly what you get, for free, if you use class inheritance – the child class can customize some, but no need to rewrite all, of the parent class.

    So, if you as much as encapsulate your “foo” code inside a class – you may not even have to write a class that needs instances, you get the features you want already:

    #foo module:
    
    class Foo(object):
        sanitize = "value 1"
        @classmethod
        def parse(cls):
            print cls.sanitize
    
    #foo2
    from foo import Foo
    class Foo2(Foo):
        sanitize = "value 2"
    
    # code:
    from foo import Foo
    from foo2 import Foo2
    
    Foo.parse()
    Foo2.parse()
    

    Of course, with the cheer amount of introspection and metaprogramming that Python allows, it would be possible to do something like what you want – but it would complicate matters, for no good. Module variables have most of the same disadvantages that global variables have in C code.

    One way to do it is to make “foo” when accessed through foo2 to make the variable changes on the fly, call the function in “foo” and restore the previosu values on exit.
    For arbitrary code to be triggered on attribute access of “foo” on the module “foo2”, “foo” has to refer to an object with a “property” attribute —

    So, the exa example you wrote would run, in a concurency-unsafe way, btw, very unsafe way, if foo2 is written more or less so:

    import foo as _foo
    
    SANITIZE = "value2"
    
    class _FooWrapper(object):
        def __getattribute__(self, attr):
            self.change_vars()
            original_function = getattr(_foo, attr)
            if callable(original):
                def wrapper(func):
                    def new_func(*args, **kw):
                        res = func(*args, **kw)
                        self.restore_vars()
                        return res
                    return new_func
                return wrapper(original)
            return original
    
        def change_vars(self):
            self.original_sanitize = _foo.SANITIZE
            _foo.SANITIZE = SANITIZE
        def restore_vars(self):
            __foo.SANITIZE = self.original_sanitize
    
    foo = _FooWrapper()
    

    This creates a “foo” object in module foo2 that when accessed, retrieves any requested attributes from the original “foo” module instead. So “foo2.foo.parse” will get the “foo” parse function – but, perceive the ammount of “hackiness” in this approach – in order to be able to restore the original value in the “foo” module that lives inside the Python interpreter, after the function was fetched from foo2, that function have too, upon returning, restore the values itself. The only way to do so is modifying the function so that it runs additional code when it returns – so it is decorated on the fly by the code above.

    I think this example makes clear that having module level configurations is not the way to go in this case, although possible.

    EDIT
    The O.P. commented:

    Thanks jsbueno, unfortunately this is not my code and I must rely on
    the module level variables. The Wrapper class method is interesting
    but as you said, very hacky and incredibly non thread safe, which I’m
    afraid won’t do for my case

    In reply to that:

    Modules are “singletons” – so, changing the variable on the module, at
    any point will make it thread unsafe. The other way I can think about
    this is creating a “photocopier” module that actually re-creates
    classes, attributes and instances of another, existing module, when
    imported – rebinding all functions global variables (methods would
    still be acessed as functions at this level)

    Reading this description it may sound as “unfeasable” – but it is easier done than described –
    Here follows my “foo2” module that does the above:

    from types import ModuleType, FunctionType
    
    import foo as _foo
    
    SANITIZE = "value 2"
    
    def rebuild_function(func, glob):
        """Rebinds the globals in the given functions to globals in 
        this module  by default"""
        new_func = FunctionType(func.func_code,
                                glob,
                                func.func_name,
                                func.func_defaults,
                                func.func_closure)
        return new_func
    
    def rebuild_class(cls, glob):
        metatype = type(cls)
        dct = cls.__dict__.copy()
        for key, value in dct.items():
            if isinstance(value, FunctionType):
                dct[key] = rebuild_function(value, glob)
        return metatype(cls.__name__, cls.__bases__, dct)
    
    def rebuild_module(mod,glob):
        new_module = ModuleType(mod.__name__)
        for key, value in mod.__dict__.items():
            if isinstance(value, FunctionType):
                value = rebuild_function(value, glob)
            elif isinstance(value, type):
                value = rebuild_class(value, glob)
            setattr(new_module, key, value)
        return new_module
    
    foo = rebuild_module(_foo, globals())
    
    __all__ = ["foo", "SANITIZE"]
    

    This code does exactly what I described – it recreates all function objects in the original module, rebindign the globals dict for each function. It is concurrency safe. There are some corner cases if the to-be clonned module does point to native code classes or functions (they are not of “FunctionType”). If it makes heavy usage of multiple class inheritance, metaclasses,- it miught work, or not.

    I tested it with a simple class and it worked fine:

    #module "foo"
    SANITIZE='foo'
    def parse():
        print SANITIZE
    
    class Parser(object):
        def __init__(self):
            print SANITIZE * 2
    

    And

    #test script
    import foo
    import foo2
    foo2.foo.parse() #i want this to print 'foo2'
    foo2.foo.Parser()
    foo.parse() #i want this to print 'foo'
    foo.Parser()
    

    Output:

    [gwidion@powerpuff tmp16]$ python test_foo.py 
    foofoo
    value 2
    value 2value 2
    foo
    foofoo
    [gwidion@powerpuff tmp16]$ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.