[Python] I tried substituting the function name for the function name

Conclusion

The assigned one will be rewritten to the assigned one. Below are the experimental results.

Colab


#Define func
def func(x, y):
  return x - y

func(1, 2) # -1


#Define func0
def func0():
  return None

func0() #No output


#Substitute the function name for the function name
func0 = func

func(1, 3) # -2

func0(1, 3) # -2

Why did you investigate this

For some reason, the __call__method of the Module class of PyTorch was set to be set with _call_impl once and then assigned to __call__ as described above. Can anyone please tell me why PyTorch made such a troublesome description (seriously). Below is the source code.

Python:torch.nn.modules.module


def _call_impl(self, *input, **kwargs):
        for hook in itertools.chain(
                _global_forward_pre_hooks.values(),
                self._forward_pre_hooks.values()):
            result = hook(self, input)
            if result is not None:
                if not isinstance(result, tuple):
                    result = (result,)
                input = result
        if torch._C._get_tracing_state():
            result = self._slow_forward(*input, **kwargs)
        else:
            result = self.forward(*input, **kwargs)
        for hook in itertools.chain(
                _global_forward_hooks.values(),
                self._forward_hooks.values()):
            hook_result = hook(self, input, result)
            if hook_result is not None:
                result = hook_result
        if (len(self._backward_hooks) > 0) or (len(_global_backward_hooks) > 0):
            var = result
            while not isinstance(var, torch.Tensor):
                if isinstance(var, dict):
                    var = next((v for v in var.values() if isinstance(v, torch.Tensor)))
                else:
                    var = var[0]
            grad_fn = var.grad_fn
            if grad_fn is not None:
                for hook in itertools.chain(
                        _global_backward_hooks.values(),
                        self._backward_hooks.values()):
                    wrapper = functools.partial(hook, self)
                    functools.update_wrapper(wrapper, hook)
                    grad_fn.register_hook(wrapper)
        return result

    __call__ : Callable[..., Any] = _call_impl

Recommended Posts

[Python] I tried substituting the function name for the function name
vprof --I tried using the profiler for Python
I tried python programming for the first time.
I tried Python on Mac for the first time.
I tried python on heroku for the first time
[Python] I tried to get the type name as a string from the type function
I tried searching for files under the folder with Python by file name
I tried to implement the mail sending function in Python
Miscellaneous notes that I tried using python for the matter
I tried tensorflow for the first time
Python: I tried the traveling salesman problem
I tried the Python Tornado Testing Framework
I tried using the python module Kwant for quantum transport calculation
I tried "smoothing" the image with Python + OpenCV
I tried using scrapy for the first time
I tried the pivot table function of pandas
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried "differentiating" the image with Python + OpenCV
I tried Python> autopep8
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I tried function synthesis and curry with python
[Python] I searched for the longest Pokemon Shiritori
I tried "binarizing" the image with Python + OpenCV
I tried using the Datetime module by Python
I tried Mind Meld for the first time
I implemented the inverse gamma function in python
I tried Python> decorator
I want to get the file name, line number, and function name in Python 3.4
I tried to graph the packages installed in Python
What I got into Python for the first time
I tried pipenv and asdf for Python version control
I tried the MNIST tutorial for beginners of tensorflow.
2017-04-11 Python> I tried generator> I was taught generator expression / generator function / next ()
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to approximate the sin function using chainer
Python program that looks for the same file name
[Python] I tried to graph the top 10 eyeshadow rankings
I tried to solve the problem with Python Vol.1
AI Gaming I tried it for the first time
I tried hitting the API with echonest's python client
I tried to summarize the string operations of Python
I tried fp-growth with python
I tried scraping with Python
About the enumerate function (python)
I tried the changefinder library!
Python #function 2 for super beginners
I tried Python C extension
[Python] I tried using OpenPose
I tried gRPC with Python
I tried scraping with python
I downloaded the python source
I tried to find the entropy of the image with python
I tried porting the code written for TensorFlow to Theano
I tried "gamma correction" of the image with Python + OpenCV
I tried to simulate how the infection spreads with Python
I was hooked for 2 minutes with the Python debugger pdb
I tried the accuracy of three Stirling's approximations in python
I tried using the Python library from Ruby with PyCall
I touched the latest automatic test tool "Playwright for Python"