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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:45:57+00:00 2026-06-12T22:45:57+00:00

I am new to python, and I don’t quite understand the __func__ in python

  • 0

I am new to python, and I don’t quite understand the __func__ in python 2.7.

I know when I define a class like this:

class Foo:
    def f(self, arg):
        print arg

I can use either Foo().f('a') or Foo.f(Foo(), 'a') to call this method. However, I can’t call this method by Foo.f(Foo, 'a'). But I accidently found that I can use Foo.f.__func__(Foo, 'a') or even Foo.f.__func__(1, 'a') to get the same result.

I print out the values of Foo.f, Foo().f and Foo.f.__func__, and they are all different. However, I have only one piece of code in definition. Who can help to explain how above code actually works, especially the __func__? I get really confused now.

  • 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-12T22:45:58+00:00Added an answer on June 12, 2026 at 10:45 pm

    When you access Foo.f or Foo().f a method is returned; it’s unbound in the first case and bound in the second. A python method is essentially a wrapper around a function that also holds a reference to the class it is a method of. When bound, it also holds a reference to the instance.

    When you call an method, it’ll do a type-check on the first argument passed in to make sure it is an instance (it has to be an instance of the referenced class, or a subclass of that class). When the method is bound, it’ll provide that first argument, on an unbound method you provide it yourself.

    It’s this method object that has the __func__ attribute, which is just a reference to the wrapped function. By accessing the underlying function instead of calling the method, you remove the typecheck, and you can pass in anything you want as the first argument. Functions don’t care about their argument types, but methods do.

    Note that in Python 3, this has changed; Foo.f just returns the function, not an unbound method. Foo().f returns a method still, still bound, but there is no way to create an unbound method any more.

    Under the hood, each function object has a __get__ method, this is what returns the method object:

    >>> class Foo(object):
    ...     def f(self): pass
    ... 
    >>> Foo.f
    <unbound method Foo.f>
    >>> Foo().f
    <bound method Foo.f of <__main__.Foo object at 0x11046bc10>>
    >>> Foo.__dict__['f']
    <function f at 0x110450230>
    >>> Foo.f.__func__
    <function f at 0x110450230>
    >>> Foo.f.__func__.__get__(Foo(), Foo)
    <bound method Foo.f of <__main__.Foo object at 0x11046bc50>>
    >>> Foo.f.__func__.__get__(None, Foo)
    <unbound method Foo.f>
    

    This isn’t the most efficient codepath, so, Python 3.7 adds a new LOAD_METHOD – CALL_METHOD opcode pair that replaces the current LOAD_ATTRIBUTE – CALL_FUNCTION opcode pair precisely to avoid creating a new method object each time. This optimisation transforms the executon path for instance.foo() from type(instance).__dict__['foo'].__get__(instance, type(instance))() with type(instance).__dict__['foo'](instance), so ‘manually’ passing in the instance directly to the function object. This saves about 20% time on existing microbenchmarks.

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

Sidebar

Related Questions

attrubuI'm new to python but don't know how to solve this: import wx class
I am new to python and don't know the best way to do this.
I'm new to python, so I don't know if this is possible. I'm trying
I'm new to python so I really don't know the language very well. the
Very new to python and can't understand why this isn't working. I have a
I will confess I'm very new to Python and I don't really know what
I am pretty new in python, and I have a problem I don't know
I am new to Python and this is my fist Python class. I am
I am newbie to python and don't know how to do this. I have
I'm fairly new to Python and don't fully understand all items and I've been

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.