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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:21:55+00:00 2026-05-13T18:21:55+00:00

>>> class A(object): pass >>> def func(cls): pass >>> A.func = func >>> A.func

  • 0
>>> class A(object): pass
>>> def func(cls): pass
>>> A.func = func
>>> A.func
<unbound method A.func>

How does this assignment create a method? It seems unintuitive that assignment does the following for classes:

  • Turn functions into unbound instance methods
  • Turn functions wrapped in classmethod() into class methods (actually, this is pretty intuitive)
  • Turn functions wrapped in staticmethod() into functions

It seems that for the first, there should be an instancemethod(), and for the last one, there shouldn’t be a wrapper function at all. I understand that these are for uses within a class block, but why should they apply outside of it?

But more importantly, how exactly does assignment of the function into a class work? What magic happens that resolves those 3 things?

Even more confusing with this:

>>> A.func
<unbound method A.func>
>>> A.__dict__['func']
<function func at 0x...>

But I think this is something to do with descriptors, when retrieving attributes. I don’t think it has much to do with the setting of attributes here.

  • 1 1 Answer
  • 2 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-05-13T18:21:55+00:00Added an answer on May 13, 2026 at 6:21 pm

    You’re right that this has something to do with descriptor protocol. Descriptors are how passing the receiver object as the first parameter of a method is implemented in Python. You can read more detail about Python attribute lookup from here. The following shows on a bit lower level, what is happening when you do A.func = func; A.func:

    # A.func = func
    A.__dict__['func'] = func # This just sets the attribute
    # A.func
    #   The __getattribute__ method of a type object calls the __get__ method with
    #   None as the first parameter and the type as the second.
    A.__dict__['func'].__get__(None, A) # The __get__ method of a function object
                                        # returns an unbound method object if the
                                        # first parameter is None.
    a = A()
    # a.func()
    #   The __getattribute__ method of object finds an attribute on the type object
    #   and calls the __get__ method of it with the instance as its first parameter.
    a.__class__.__dict__['func'].__get__(a, a.__class__)
    #   This returns a bound method object that is actually just a proxy for
    #   inserting the object as the first parameter to the function call.
    

    So it’s the looking up of the function on a class or an instance that turns it into a method, not assigning it to a class attribute.

    classmethod and staticmethod are just slightly different descriptors, classmethod returning a bound method object bound to a type object and staticmethod just returns the original function.

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

Sidebar

Related Questions

ok, ive a class and i pass an object as property. the object that
I am doing something like this: class Class(object): def __init__(self): self.var=#new instance name string#
I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1,
Why doesn`t this work: class X: var1 = 1 def __enter__(self): pass def __exit__(self,
I get this warning from GCC: warning: cannot pass objects of non-POD type 'class
How can I access Class object for Generics? Currently, I am doing it this
I create a pointer-to-pointer of a class object and when I try to create
In .NET, if a class contains a member that is a class object, should
I would like to do the following: class A(object): pass a = A() a.__int__
This works now for those new to this question: class ensureparams(object): Used as a

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.