https://qiita.com/ganariya/items/fb3f38c2f4a35d1ee2e8
Apparently all Python is an object.
?? ?? ?? ?? ?? Really? ?? ?? ?? ??
A Python object is anything that Python handles. And the object has ** attributes **.
For example
class A:
def __init__(self):
self.x = 50
a = A()
print(a.x)
In the above code, the value is fetched using "period" like a.x. It is "** attribute **" (x) that can be retrieved using this period, and having an attribute also means ** object ** (a).
class A:
def __init__(self):
self.x = 50
a = A()
print(type(a))
print(type(A))
print(type(30))
print(type(int))
'''
<class '__main__.A'>
<class 'type'>
<class 'int'>
<class 'type'>
'''
Looking at the above example, let's see that all Python data is actually an object.
The above code uses a built-in function called type
.
type is a function that returns the type of the object passed as an argument.
For example, the object a is of the type A of the main module. In addition, the class A, which is the original type, has a type called type. Similarly, the Python number 30, is of type int, and we know that 30 is an instance object of int.
As you can see, all Python data is apparently made up of objects.
--Class object --Instance object
It seems to be composed of.
So what is a class object?
As the name implies, a class object is
Refers to the object of that class
when you have finished defining the class.
For example
class A:
x = 50
print(10)
def __init__(self):
self.x = 100
print(self.x)
print(A.x)
a = A()
Consider the above sources.
What will happen to the result of executing this?
this is
10
50
100
It looks like.
Python classes are executed as they are, except for def, and each line is executed like a normal statement.
So if you find class A when reading a Python file from above Go down as it is
x = 50
print(10)
And after loading class A, create a class object called A. Therefore, A becomes a ** class object ** when the reading is finished.
It is important that it is a class object.
Instance objects are objects created from "class objects".
In particular
--30 (generated from int) --a (generated from A) --Function
And so on.
Because it is generated from a class object
class A:
def __init__(self):
self.x = 100
a = A()
print(type(a))
print(type(30))
print(type(int))
'''
<class '__main__.A'>
<class 'int'>
<class 'type'>
'''
a is a class object main. A that is a type of a 30 is a class object int of type 30
It has become.
In this way, instance objects are created from class objects.
Functions are also created from a class object called the function class.
Functions are objects as well. Objects can use attributes. Especially because the function is an instance object whose attributes can be set freely.
For example
--Check only once to see if matplotlib can be loaded
You can create a function called.
def can_load_matplotlib():
if can_load_matplotlib.cache is None:
try:
import matplotlib
except ImportError:
can_load_matplotlib.cache = False
else:
can_load_matplotlib.cache = True
return can_load_matplotlib.cache
can_load_matplotlib.cache = None
print(can_load_matplotlib())
Prepare the can_matplotlib function.
The Python file only creates the identity (memory) that "the function is on this line ~ ~" until the function is executed.
So, first of all, in the above code
--There is a function called can_load_matplotlib. --can_load_matplotlib is an instance object of Function class, and since attributes can be set freely from the outside, add cache attribute. --When you execute the function, it will try internally because it is None for the first time. --As a result, the call can be cached in a variable depending on whether it can be called successfully or not.
You can write the code.
I met this code when I copied the code of an overseas type of GitHub, and I thought it was amazing. But how much speed does caching increase? I don't know.
In Python, we've seen that classes, numbers, and strings are all objects.
I would like to write while distinguishing between an instance and a class object.
-How to check the variable type of Python -Type function to get / determine type in Python, isinstance function
Recommended Posts