Skip to content Skip to sidebar Skip to footer

Changing Local Variables Inside Functions Or Methods In Python

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 direc

Solution 1:

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).


Solution 2:

Create a patch and send it to library authors. They will apply it to their code base and be grateful for your involvment. Just do it in a way that it can be configurable - like add new method that creates that dictionary, you'd like to extend. Then you could override that new method in your subclass and the patch won't be specific to your project.

If library owners merge your patch, you'll be able to just use newer version of library, once it's released.


Solution 3:

As said by Ignacio Vazquez-Abrams, that's what Git is for and seems the best advice. But not an answer to the question. I still wanted to let other users see this, if they are struggling with

We are currently using git rebase, so when a new release comes out we just do a git

git checkout our_changes_to_upstream_branch
git rebase -i upstream/master

on our modified branch. This will show a list of commits we've done and let's us manually choose which commits are no longer needed. After that it will remove our changes and save it temporary to somewhere in git, then do a fast forward with upstream and after it's at the latest version of upstream, it will replay our changes on top of the latest version. If there are conflicts it will stop and you can specify if you want to manually merge or skip.

Normally at this point if there is a conflict we just skip it and create a ticket to the original commit author to make a new patch and see if the patch is still needed.

We noticed that most patches need a rewrite but that this is the cleanest way to do it. If there are any improvements would be glad to hear them!


Post a Comment for "Changing Local Variables Inside Functions Or Methods In Python"