I’m extending an open source python product, but I can’t change the code directly as that would make merging with upstream changes harder.
So instead of changing the function directly I want to manipulate the local variable inside the function from another place. I want to add a key to a dictionary which is created inside the function as local variable. How would I be able to change this local dic variable? Is this possible?
Take as example the below code:
import functools
class API(object):
def update_resources(self, host):
bla = sflkj
dic = {
'cpu': 5,
'memory': 500
}
class APIExtension(API):
def update_resources(self, host):
# But first do our changes to the function here
# The change that I want to do is add a key: 'workload'
# this is the function I want to change the dic variable to include workload
super(APIExtension, self).update_resources
You can’t. If the variable is local to the function it doesn’t exist except when the function is executing. Even if you could, it’s unlikely that managing this hack would be easier than managing your custom changes to the library (or asking the library developers to change it so the variable is made accessible).