Why isn't self passed once an attribute is changed to refer to an external function?

Algorithmic Canary

If one changes a object attribute which refers to an internal method to an external function after the object is created, self is not passed to the new function, even if the attribute (which contains a function) is called like before.

class Person:
    def greet(self):
        print("hello")

    do = greet

def wave(person):
    print("bye")

alice = Person()
alice.do() #prints 'hello'

#change do to point an external function
alice.do = wave
alice.do() #Error: Missing argument

The exact error which I get is:

hello
Traceback (most recent call last):
  File "C:\Users\Owner\Desktop\miniLiveMethodSwitch.py", line 15, in <module>
    alice.do() #Error: Missing argument
TypeError: wave() missing 1 required positional argument: 'person'

If one move the external function into the class as a internal method,

class Person:
    def greet(self):
        print("hello")

    def wave(person):
        print("bye")

    do = greet

Then the code works as expected. Why isn't self passed once the attribute is changed to refer to an external function? What is the proper way to call the function so that self is passed?

user2357112 supports Monica

When Python finds alice.do in the class dict, it invokes the descriptor protocol, calling the function's __get__ method to produce a bound method object that will be the result of the alice.do lookup. This bound method object knows what self should be and will automatically insert the self argument into the argument list when forwarding arguments to the underlying function. (This happens positionally, not by the self name.)

When Python finds alice.do in alice's instance dict, the descriptor protocol is not invoked, and alice.do is just the raw function. self is not injected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why isn't the value of list passed in a function getting updated?

Why this implementation of a pure function isn't considered to have external dependencies?

Why isn’t data passed to the view in Laravel?

Why isn't the array being passed by reference?

Why isn't the label text changed instantly?

matplotlib funcAnimation isn't calling the passed function

Arguments isn't passed on correctly to function in R

Rust: Why isn't "self" a reference when "self: &Self"?

Why function that is property of the object isn't accessible once we add the same function to its prototype?

Why isn't #pragma once automatically assumed?

Common lisp — why isn't this symbol external?

Why isn't my object attribute populated?

Why isn't this protected attribute working?

Why isn't my js function for setting the css display attribute to inline for an img tag working?

Why isn't my for loop incrementing and why isn't my splice being changed?

Why isn't the props/actions passed the first time?

Why isn't my prop logging or being passed in React Native?

Why isn't state being passed to child components?

Reference Assignment in Python - Why isn't the value changed?

Why isn't clear button function isn't working?

Python: Global variable isn't getting affected/changed by function

Why isn't the state of a transition maintained once it ends?

Why isn't this anonymous function returning?

Why isn't this factorial function returning?

Why isn't my function returning the string?

Why isn't function call an lvalue

JavaScript permutations function - why isn't this working?

Why isn't the type() function working here?

Why is [0] a different function but 0 isn't?