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 8650725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:53:35+00:00 2026-06-12T13:53:35+00:00

I have a utility method that behaves like this def my_patch_method(self): pass def patch_my_lib():

  • 0

I have a utility method that behaves like this

def my_patch_method(self):
    pass

def patch_my_lib():
    from mylib import MyClass
    MyClass.target_method = my_patch_method
    return MyClass()

This test fails:

self.assertEqual(my_patch_method, patch_my_lib().target_method)

Whereas this one works:

self.assertEqual(my_patch_method.__name__, patch_my_lib().target_method.__name__)

As the patch method does not have the same name, this is still acceptable proof that patch_my_lib() is doing what it’s payed for but why doesn’t the first work as I would expect ? And, is there a way to “fix” it ?

  • 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-06-12T13:53:36+00:00Added an answer on June 12, 2026 at 1:53 pm

    The reason your first test fails is that once you monkey-patch the function into your class, it isn’t really the same object any more.

    >>> def foo(self): pass
    ... 
    >>> class Foo: pass
    ... 
    >>> Foo.bar = foo
    >>> type(Foo.bar)
    <type 'instancemethod'>
    >>> type(foo)
    <type 'function'>
    >>> 
    >>> Foo.bar is foo
    False
    >>> Foo.bar == foo
    False
    

    In fact, the original function and the new method have different types. Instead, have your first test check this condition:

    >>> Foo.bar.im_func is foo
    True
    

    So maybe this: self.assertIs(my_patch_method, patch_my_lib().target_method.im_func)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a utility method which returns a strongly typed value from an old
I have a utility build script that gets called from a variety of project-specific
I have created a utility method that contains some try/catches in it. In those
I have a utility method (= a static one) that I call a lot,
I have an action-method in a controller that takes requests coming from a variety
I have a utility method that uses Display Templates to generate HTML: public static
I am currently trying to call a method from a utility class that will
I am validating some input from a Restlet URL in a utility method (that
I have a utility method that goes through various classes and recursively retrieves the
I have a small utility extension method that performs some null checks on some

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.