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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:00:22+00:00 2026-06-14T20:00:22+00:00

Let’s start with some code: def func(*x): print(‘func:’, x) class ABC: def __init__(self, f):

  • 0

Let’s start with some code:

def func(*x):
    print('func:', x)


class ABC:
    def __init__(self, f):
        self.f1 = f

    def f2(*x):
        print('f2:', x)

Now we do some tests:

>>> a = ABC(func)
>>> a.f1(10)
func: (10,)
>>> a.f2(10)
f2: (<__main__.ABC object at 0xb75381cc>, 10)
>>> a.f3 = func
>>> a.f3(10)
func: (10,)
>>> a.f1
<function func at 0xb74911ec>
>>> a.f2
<bound method ABC.f2 of <__main__.ABC object at 0xb75381cc>>
>>> a.f3
<function func at 0xb74911ec>

Note that func is a normal function and we are making it a method f1 of the class.

We can see that f2 is getting the class instance as the first argument, but f1 and f3 are not, even though all functions are called as class methods. We can also see that if we call a normal function as a method of a class, Python does not make a bound method from it.

So why is f1 or f3 NOT getting a class instance passed to it even when we are calling it as a method of a class? And also, how does Python know that we are calling an outer function as a method so that it should not pass an instance to it.

— EDIT —

OK, so basically what I am doing wrong is that I am attaching the functions on the instance and NOT on the class object itself. These functions therefore simply become instance attributes. We can check this with:

>>> ABC.__dict__
... contents...
>>> a.__dict__
{'f1': <function func at 0xb74911ec>, 'f3': <function func at 0xb74911ec>}

Also note that this dict can not be assigned to:

>>> ABC.__dict__['f4'] = func
TypeError: 'dict_proxy' object does not support item assignment
  • 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-14T20:00:23+00:00Added an answer on June 14, 2026 at 8:00 pm

    You kind of partially answered your own question inspecting the object. In Python, objects behave like namespaces, so the first attribute points to a function and the second points to a method.

    This is how you can add a method dynamically:

    from types import MethodType
    
    def func(*x):
        print('func:', x)
    
    
    class ABC:
        def __init__(self, f):
            self.f1 = MethodType(f, self, self.__class__)
    
        def f2(*x):
            print('f2:', x)
    
    if __name__ == '__main__':
        a = ABC(func)
        print a.f1(10)
        print a.f2(10)
        a.f3 = MethodType(func, a, ABC)
        print a.f3(10)
    

    Note that it will bind the method to your instance, not to the base class. In order to monkeypatch the ABC class:

    >>> ABC.f4 = MethodType(func, None, ABC)
    >>> a.f4(1)
    ('func:', (<__main__.ABC instance at 0x02AA8AD0>, 1))
    

    Monkeypatching is usually frowned upon in the Python circles, despite being popular in other dynamic languages (notably in Ruby when the language was younger).

    If you ever resort to this powerful yet dangerous technique, my advice is:

    • never, ever override an existing class method. just don’t.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's assume that a user votes for some movies in a scale of 1
Let's say I have some text as follows: do this, do that, then this,
Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have the following classes : public class MyProductCode { private String
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say there is a graph and some set of functions like: create-node ::
let ans = stringConcat [<a href=,\,str,\,>,strr,</a>] putStr (\nOutput : ++show (ans)) when I print
Let's say I have some data in a CouchDB database. The overall size is

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.