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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:45:46+00:00 2026-06-08T21:45:46+00:00

class C: def func(self, a): print(a) c = C() print(c.__dict__) # {} c.func =

  • 0
class C:
  def func(self, a):
    print(a)

c = C()
print(c.__dict__) # {}
c.func = c.func
# c.func is now an instance attribute, which hides instance method
print(c.__dict__) # {'func' : (bound method C.f of (__main.C object at ...))}
# how come it still works as if it's an instance method:
c.func(1) # prints 1

# with a regular function assigned to c.func, this won't happen:
def newfunc(self, a):
  print(a)
c.func = newfunc
c.func(1) # TypeError

What are the conditions under which a call c.func(1) magically adds the instance c as the first parameter? I thought it only happens when func was defined as an instance method in the class. But it seems it sometimes works when func is just an instance attribute.

  • 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-08T21:45:48+00:00Added an answer on June 8, 2026 at 9:45 pm

    The binding to the instance happens when you access c.func, not when you call it. When you do c.func = c.func, the attribute access on the right-hand side is handled by the class C and evalutes to a bound method. Reassigning it to c.func assigns it to the instance, but it’s already a bound method, so this doesn’t change the behavior.

    In your newfunc example, you just defined a function and assigned it, so it never got made into an instance method. Since you assign it directly to an instance, the class doesn’t get a chance to intercept the access and wrap it in an instancemethod.

    You can assign a bound method anywhere you like and it remains bound to the instance via which you got it. See these examples:

    >>> class C(object):
    ...     def __init__(self, name):
    ...         self.name = name
    ...     def func(self, a):
    ...         print("Called {0} with {1}".format(self.name, a))
    >>> one = C("one")
    >>> two = C("two")
    >>> one.func(1)
    Called one with 1
    >>> two.func(1)
    Called two with 1
    >>> one.func2 = two.func
    >>> one.func2(1)
    Called two with 1
    

    Note that once I assigned one.func2 = two.func, calling one.func2 calls an instance method bound to two, not one. This is because when I accessed two.func, I got a method bound to that instance. It doesn’t matter where I assign it later. It remains bound to the instance via which it was accessed.

    If you access the unbound method directly and try to assign it elsewhere, then you get the TypeError because the method was never bound:

    >>> one.func3 = C.func
    >>> one.func3(1)
    Traceback (most recent call last):
      File "<pyshell#16>", line 1, in <module>
        one.func3(1)
    TypeError: unbound method func() must be called with C instance as first argument (got int instance instead)
    

    Also note that if you add the method to the class, it will work when called on an instance, even if the instance was created before you added the method to the class. Keeping the same one object from before, I can do:

    >>> def newFunc(self, a):
    ...     print("Newfunc of {0} called with {1}".format(self.name, a))
    >>> C.newFunc = newFunc
    >>> one.newFunc(1)
    Newfunc of one called with 1
    

    To answer your question succinctly, c.func is bound to c if c is an instance of a class that has a method func, and c does not itself have an instance attribute func. (If c did have an instance attribute of the same name, that overrides the class one, so the class doesn’t get to do its wrapping to bind the method to the instance.)

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

Sidebar

Related Questions

I have a method which calls for a classmethod of another class def get_interface_params_by_mac(self,
class fcount(object): def __init__(self, func): self.func = func self.count = 0 self.context_count = 0
I have the following situation in my python code: class Parent(object): def run(self): print
Here's an example of what I mean: class MyDecorator(object): def __call__(self, func): # At
I get this error object has no attribute 'im_func' with this class Test(object): def
class Packagings: def _init_(self): self.length,self.deckle,self.tmp,self.flute,self.gsm,self.t_weight,self.weight def read_values(self): print Select Type Of Paper 1.3 Ply
class Packagings: def __init__(self): self.length=0 self.deckle=0 self.tmp=0 self.flute=[] self.gsm=[] self.t_weight=0 self.weight=0 def read_values(self): print
class Test: def func(): print('func') test1 = Test() test2 = Test() test1.func() #TypeError: fun1()
Try running the following code: class Test(object): def func_accepting_args(self,prop,*args): msg = %s getter/setter got
Assume I have some simple class class TestClass: def doSomething(self): print 'Did something' I

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.