How to do "Lending and borrowing JS methods (the one who uses apply)" in Python

Assumption, in JavaScript

In JavaScript, it is well known that you can change the context of this in function through apply.

So to speak, methods of other classes can be used at runtime.

After going around, it seems that it is called the borrowing method in English. Does it literally mean "borrow"?

Example


function A () {
  this.value = 1;
}


function B () {
  this.value = 2;
}


B.prototype.show = function show () {
  console.log(this.value);
};


var a = A();
B.prototype.show.apply(a);  //It's like "a borrows and uses show, which is a method of B."

Execution result


1

The main subject, in Python

Today, while eating with someone I hadn't seen in a long time, I was asked, "Can I do the same with Python?" In Python I want to change `` `self``` at runtime. From the conclusion, "I can". I couldn't explain it on the spot, so I decided to write it in Qiita.

In python


class A(object):

    def __init__(self):
        self.value = 1


class B(object):

    def __init__(self):
        self.value = 2

    def show(self):
        print(self.value)


a = A()
B.__dict__['show'](a)

Execution result


1

Summary

It was said that in Python you can refer to various things through __dict__.

However, if you use this too much, it will become black magic, so stop using it normally.

Recommended Posts

How to do "Lending and borrowing JS methods (the one who uses apply)" in Python
[Python] How to do PCA in Python
How to display bytes in the same way in Java and Python
How to do R chartr () in Python
How to use is and == in Python
How to write the correct shebang in Perl, Python and Ruby scripts
How to get the date and time difference in seconds with python
How to use the C library in Python
How to generate permutations in Python and C ++
How to get the files in the [Python] folder
How to plot autocorrelation and partial autocorrelation in python
Tips for those who are wondering how to use is and == in Python
How to retrieve the nth largest value in Python
How to get the variable name itself in python
Receive the form in Python and do various things
How to get the number of digits in Python
How to do hash calculation with salt in Python
How to know the current directory in Python in Blender
How to define Decorator and Decomaker in one function
How to do zero-padding in one line with OpenCV
% And str.format () in Python. Which one do you use?
[Python] How to sort dict in list and instance in list
How to use the model learned in Lobe in Python
[Python] How to output the list values in order
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
[Note] How to write QR code and description in the same image with python
How to check in Python if one of the elements of a list is in another list
[python] How to check if the Key exists in the dictionary
How to debug the Python standard library in Visual Studio
How to swap elements in an array in Python, and how to reverse an array.
Foreigners talk: How to name classes and methods in English
How to use the __call__ method in a Python class
[Python] How to write an if statement in one sentence.
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to do Bulk Update with PyMySQL and notes [Python]
Comparison of how to use higher-order functions in Python 2 and 3
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary
How to develop in Python
How to execute external shell scripts and commands in python
How to log in to AtCoder with Python and submit automatically
Determine the date and time format in Python and convert to Unixtime
How to determine the existence of a selenium element in Python
How to install OpenCV on Cloud9 and run it in Python
How to pass and study the Python 3 Engineer Certification Basic Exam
How to know the internal structure of an object in Python
How to give and what the constraints option in scipy.optimize.minimize is
The 15th offline real-time how to write reference problem in Python
How to get followers and followers from python using the Mastodon API
The 17th Offline Real-time How to Solve Writing Problems in Python
How to check the memory size of a variable in Python
[Python] How to get the first and last days of the month
How to use functions in separate files Perl and Python versions
How to judge that the cross key is input in Python3
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
Difference in how to write if statement between ruby ​​and python
[ROS2] How to describe remap and parameter in python format launch
The 14th offline real-time how to write reference problem in python