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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:02+00:00 2026-06-17T16:58:02+00:00

In Python, I want to mock the __init__ method of a class using the

  • 0

In Python, I want to mock the __init__ method of a class using the minimock library.

This is what the interpreter does (ipython):

In [1]: import minimock

In [2]: class A:
   ...:     def __init__(self):
   ...:         print "REAL INIT"
   ...:

In [3]: def new_init(self):
   ...:     print "NEW INIT"
   ...:

In [4]: minimock.mock("A.__init__", returns_func=new_init)

In [5]: a = A()
Called A.__init__()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-144b248f218a> in <module>()
----> 1 a = A()

D:\Tools\Python27\lib\site-packages\minimock\__init__.pyc in __call__(self, *args, **kw)
    492         if self.mock_tracker is not None:
    493             self.mock_tracker.call(self.mock_name, *args, **kw)
--> 494         return self._mock_return(*args, **kw)
    495
    496     def _mock_return(self, *args, **kw):

D:\Tools\Python27\lib\site-packages\minimock\__init__.pyc in _mock_return(self, *args, **kw)
    505                 raise Exception("No more mock return values are present.")
    506         elif self.mock_returns_func is not None:
--> 507             return self.mock_returns_func(*args, **kw)
    508         else:
    509             return None

TypeError: new_init() takes exactly 1 argument (0 given)

However, if I remove the self argument from new_init:

def new_init():
    print "NEW INIT"

Instantiating the A class gives:

In [13]: a = A()
Called A.__init__()
NEW INIT

Which leads me to think that minimock has limitations with “self”.

Do you know if it is possible to use “self” in the mocked versions of methods passed to minimock?

  • 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-17T16:58:03+00:00Added an answer on June 17, 2026 at 4:58 pm

    I really don’t understand why you are doing that, anyway the answer is that you should not call A.__init__ directly:

    >>> class A(object):
    ...     def __init__(self):
    ...             print('Old init - self' + str(self))
    >>> def new_init(self):
    ...     print('New init - self:' + str(self))
    ... 
    >>> A.__init__ = new_init
    >>> A.__init__()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unbound method new_init() must be called with A instance as first argument (got nothing instead)
    >>> A()
    New init - self:<__main__.A object at 0x2446d90>
    <__main__.A object at 0x2446d90>
    

    The minimock library does nothing wrong. __init__ is meant to be called by calling the class. If you call it explicitly then you must pass explicitly an instance as first argument.

    Anyway I’d strongly suggest you to think again of what you are doing because you shouldn’t do something like this. Mocks should be used to mock entire objects, not just a method.
    In your case I’d mock the entire class A and not only it’s __init__ method.

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

Sidebar

Related Questions

I am using the python mock framework for testing (http://www.voidspace.org.uk/python/mock/) and I want to
Using Python I want to create a property in a class, but having the
Using Python I want to randomly rearrange sections of a string based on a
Using Python I want to replace all URLs in a body of text with
Using Python I want to create a symbolic link pointing to a path that
I'm using python and want to embed gtk3 widgets in clutter stage. However, it
i've got a problem with python: I want to assign a method to an
Possible Duplicate: split a string in python I want to change this: str =
Possible Duplicate: Creating a simple XML file using python I want to write a
In python I want to diff two files using commands.getoutput('diff a.txt b.txt') and if

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.