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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:44:33+00:00 2026-05-23T08:44:33+00:00

Based on my understanding of Python’s data model , and specifically the subsection Instance

  • 0

Based on my understanding of Python’s data model, and specifically the subsection “Instance Methods”, whenever you read an attribute whose value is of type “user-defined function”, some magic kicks in and you get a bound instance method instead of the actual, original function. That magic is why you don’t explicitly pass the self parameter when you’re calling a method.

But then, I would expect to be able to replace an object’s method with a function with the same signature:

class Scriptable:
    def __init__(self, script = None):
        if script is not None:
            self.script = script   # replace the method
    def script(self):
        print("greetings from the default script")

>>> scriptable = Scriptable()
>>> scriptable.script()
greetings from the default script

>>> def my_script(self):
...     print("greetings from my custom script")
...
>>> scriptable = Scriptable(my_script)
>>> scriptable.script()
Traceback (most recent call last):
  ...
TypeError: script() takes exactly 1 positional argument (0 given)

I’m creating an instance of Scriptable, and setting its script attribute to a user-defined function with a single parameter, just like what’s defined in the class. So when I read the scriptable.script attribute, I would expect the magic to kick in and give me a bound instance method that takes no parameters (just like I get when I didn’t replace script). Instead, it seems to be giving back the exact same function I passed in, self parameter and all. The method-binding magic isn’t happening.

Why does the method-binding magic work when I define a method inside the class declaration, but not when I assign the attribute? What makes Python treat these situations differently?

I’m using Python3 if it makes any difference.

  • 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-05-23T08:44:34+00:00Added an answer on May 23, 2026 at 8:44 am

    Here is how you do it:

    import types
    class Scriptable:
        def __init__(self, script = None):
            if script is not None:
                self.script = types.MethodType(script, self)   # replace the method
        def script(self):
            print("greetings from the default script")
    

    As ba__friend noted in the comments, methods are stored on the class object. A descriptor on the class object returns functions as bound methods when you access the attribute from a instance.

    When you assign a function to a instance nothing happens special happens, so you have to wrap the function yourself.

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

Sidebar

Related Questions

I'm trying to get a deeper understanding in Python's data model and I don't
based on my understanding WCF's data contract model was opt-in vs the old opt-out
Based on my understanding of the Java language, static variables can be initialized in
Normally ( Based on my understanding ) i have to follow a lot of
From my understanding the XMPP protocol is based on an always-on connection where you
My understanding is that Java's implementation of regular expressions is based on Perl's. However,
This question probably is based on my lack of understanding of the role of
I've been working with some ExternalDataExchange - based communication in WF recently. My understanding
Based on a few posts I've read concerning version control, it seems people think
I have some doubt about python's class variables. As my understanding, if I define

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.