4

This maybe a very naive question and perhaps it's best asked with an example:

module1.py

import module2

def new_func():
    print(var_string)
module2.new_func = new_func

module2.func()
module2.new_func()

module2.py

var_string = "i'm the global string!"

def func():
    print(var_string)

result

> python module1.py
i'm the global string!
Traceback (most recent call last):
  File "module1.py", line 8, in <module>
    module2.new_func()
  File "module1.py", line 4, in new_func
    print(var_string)
NameError: name 'var_string' is not defined

So my question is this: Is it possible to insert a function into a module and have it's global namespace update accordingly?

Related: global variable defined in main script can't be accessed by a function defined in a different module Note that I am aware that sharing global variables is a bad idea and I'm also aware that a configuration module would be a good compromise to that but also please note that this is not what I am trying to achieve.

6
  • Their is no need to do this ever. You can just use if statements to determine which method to call, or what arguments to pass into a method. etc... Commented Jul 29, 2015 at 14:55
  • I disagree. I think this would be very useful. An analogy is dynamically adding a method to an existing class which utilizes other attributes and methods of the class. I also want this inserted function to be callable by other modules. In essence, I am dynamically extending the module without physically editing the module's source code. Commented Jul 29, 2015 at 14:59
  • You need to change print(var_string) to print(module2.var_string) Commented Jul 29, 2015 at 15:04
  • @DamiánMontenegro No, that is not what I'm looking for. I want func to have direct access to module2's global members as if it were defined originally in global2. Commented Jul 29, 2015 at 15:04
  • I've never had to do this. But is method overriding what you're talking about? Check out this link lgiordani.com/blog/2014/05/19/method-overriding-in-python/… Commented Jul 29, 2015 at 15:04

2 Answers 2

1

You may think it is useful, but very little python code is written this way and I think most python programmers would be confused with code that does this. Modifying a module after it's been imported (monkeypatching) is usually looked down upon because it's very easy to do the wrong thing and cause strange bugs.

You made an analogy comparing it to overriding / extending methods on a class, but if this is really what you want to do, why not just use a class? The features of classes make it much safer and easier to do this kind of thing.

Your code will work if you do this:

from module2 import var_string
#or..
from module2 import *

But I'm not sure if that's the solution you're looking for. Either way, I personally wouldn't try to make this code work, it's fighting against the way python code is normally written. If you have a practical example of code that you think would be improved by dynamically modifying modules, I would like to see it. It's a little hard to see the benefit with the example code you gave.

Sign up to request clarification or add additional context in comments.

3 Comments

Fair point. Some context: I am using the logging module and I wanted to add a function to the module (which uses other stuff in the module) which I can then reference from other modules in my application. Here's the original question which prompted me to ask this. stackoverflow.com/questions/31701470/…
Steven, 8 years later, another example: A FOSS library we are using is missing a single function - we cannot contribute it to this library since it is a special case that doesn't generalize well, but we still want to use that library instead of forking it (and having to manually update it). We are aware that this code might break at some point, but this is easier than always having to keep the fork up to date.
@Thomas That's a fine use case for doing this. It definitely has its place in real world systems. I've had to do similar things in the past e.g. in legacy systems where we lost the ability to update third party packages.
0

I'm not understand what you want, and what this string must do "module2.new_func = new_func", because you dont have the function new_funcin module2. But if you want to reseting variable in each modules, you cant use like this :

Module1 :

import module2

def new_func():
    print(var_string)

new_class=module2.MyStuff()
var_string=new_class.func()
new_func()

Module2:

class MyStuff:

    def __init__(self):
        self.var_string  = "i'm the global string!"

    def func(self):
        print(self.var_string)
        return self.var_string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.