1

My goal: pass a reference to a function, and be able to update the function later on.

In Python, I believe functions are passed by reference, but a function's reference is considered immutable. Thus, the function can't be updated later on.

This question gets at the core problem: Python functions call by reference

The answers all point at the workaround: pass the function within a mutable type such as a list or a dict.

I am wondering: is there a more direct way? Might there be some functools function, types utility, or external library that enables this behavior?


Sample Code

**Update**: I am not interested in making foo._on_call public. I am trying to change on_call externally via a reference, not by actually operating directly on the foo object.

from typing import Callable, List

class Foo:
    def __init__(self, on_call: Callable[[], int]):
        self._on_call = on_call

    def __call__(self) -> int:
        return self._on_call()

def do_something() -> int:
    return 0

def do_something_else() -> int:
    return 1

foo = Foo(do_something)

do_something = do_something_else  # Updating do_something
print(do_something())  # This prints 1 now
print(foo())  # This still prints 0, I want this to print 1 now too

Known (undesirable) workarounds:

  • Pass do_something within a list
  • Recreate the foo object
  • Making Foo's _on_call attribute public or using a property decorator

Python version: 3.8

2
  • more direct way: foo._on_call = do_something_else
    – furas
    Commented Feb 6, 2021 at 23:03
  • 1
    Could you give some more context? This seems like it could be an XY problem.
    – wjandrea
    Commented Feb 7, 2021 at 20:49

2 Answers 2

3

Assigning do_something = do_something_else outside of Foo does not change the function that was known as do_something, it just points the name do_something in the module scope to a different function. The name _on_call in the Foo instance's scope is not affected by that and still points to the original function.

The simplest and most idiomatic way to achieve what you want is to make _on_call a public attribute of a Foo instance, or add a method that updates it:

1. attribute

class Foo:
    def __init__(self, on_call):
        self.on_call = on_call
    # ...

foo = Foo(do_something)
# ...
foo.on_call = do_something_else

(You do not have to rename the attribute to be able to do that, but it's good style to treat attributes beginning with an underscore like they are not supposed to be accessed from outside the class.)

2. method

class Foo:
    def __init__(self, on_call):
        self._on_call = on_call
    def update(self, on_call):
        self._on_call = on_call
    # ...

foo = Foo(do_something)
# ...
foo.update(do_something_else)
4
  • 1
    FWIW, another option is making it a property with a setter. This is just like an attribute but with more flexibility.
    – wjandrea
    Commented Feb 6, 2021 at 23:07
  • Thank you @mkrieger1 for proposing a workaround. However, I am trying to figure out if I can pass a function by reference without using list or dict, not working around it by changing the exposed interface of Foo Commented Feb 6, 2021 at 23:42
  • No, you can't do that.
    – mkrieger1
    Commented Feb 6, 2021 at 23:42
  • Well, you can sneakily assign foo._on_call = do_something_else, since _on_call is not considered part of the public interface, but that would be bad style.
    – mkrieger1
    Commented Feb 6, 2021 at 23:48
1

Functions are actually mutable, via __code__. Here's another example. However, don't know if this is a good idea.

do_something.__code__ = do_something_else.__code__
print(do_something())  # -> 1
print(foo())  # -> 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.