0

First of all, here is my (pseudo) code:

somemodule.py:

class parentclass(object):
 def __init__(self):
 if(not prevent_infinite_reursion) #Just to make shure this is not a problem ;)
 self.somefunction()
 def somefunction():
 # ... deep down in a function ...
 # I want to "monkey patch" to call constructor of childclass, 
 # not parentclass
 parentclass() 

othermodule.py

from somemodule import parentclass
class childclass(parentclass):
 def __init__(self):
 # ... some preprocessing ...
 super(childclass, self).__init__()

The problem is, i want to monkey patch parent class, so it would call constructor of childclass, without changing code of somemodule.py. It doesn't matter if it's patched only in class instance(that's shure better) or globally.

I know i can override somefunction, but it contains too many lines of code, for this being sane.

Thanks!

asked May 13, 2013 at 7:56
7
  • You want all attempts to construct a parentclass to instead construct a childclass, or only attempts within parentclass, or only this specific attempt within parentclass.somefunction, or one of the latter two but only when self is a childclass? Commented May 13, 2013 at 8:06
  • Well any of this, so i will appreciate any kind of answer, but the best would be attempts in parentclass. Commented May 13, 2013 at 8:10
  • Actually, that's probably the most difficult possibility. But let me think about it... Commented May 13, 2013 at 8:10
  • I covered this in my very first answer on StackOverflow, though it replaces all occurances of parentclass(). Commented May 13, 2013 at 8:11
  • 1
    I can think of two horribly hacky things that might work: (1) Replace each method of parentclass with a wrapper that patches out __new__, calls the original, and restores __new__. (2) Replace each method of parentclass with a wrapper that runs the original inside a modified globals where parentclass has been replaced by childclass. But I don't think either of these is a good enough idea to be worth coding. Commented May 13, 2013 at 8:15

1 Answer 1

3

You could use mock.patch for this:

class childclass(parentclass):
 def somefunction(self):
 with patch('somemodule.parentclass', childclass):
 super(childclass, self).somefunction()
answered May 13, 2013 at 8:36
Sign up to request clarification or add additional context in comments.

4 Comments

A nice clean way to do my "horribly hacky thing (1)".
Hm it doesn't work in my case, because parentclass is eventually a children of some other class and parent of parentclass new method gets called and it creates parentclass instead of supposedly patched child class, will try to find how to manage that.
@offlinehacker You can patch the __new__ method as well. Also it might help to read where to patch.
@LauritzV.Thaulow eventually it worked like a charm with patching __new__ of a top class. This one was hard. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.