[PYTHON] Add the attribute of the object of the class with the for statement

If you want to create multiple attributes of an object, you can use the built-in function setattr. There are three arguments: the object to which the attribute is added, the attribute name, and the value.

import numpy as np

class Test:

test = Test()
for i in range(10):
    setattr(test, 'var' + str(i), np.random.randint(10))


for name, value in test.__dict__.items():
    print(f'{name} : {value}')


var0 : 2
var1 : 6
var2 : 2
var3 : 1
var4 : 2
var5 : 2
var6 : 7
var7 : 2
var8 : 7
var9 : 4

Recommended Posts

Add the attribute of the object of the class with the for statement
The third night of the loop with for
The second night of the loop with for
[Introduction to Python] How to get the index of data with a for statement
Add information to the bottom of the figure with Matplotlib
Decrease the class name of the detection result display of object detection
[For beginners] Quantify the similarity of sentences with TF-IDF
Save the output of conditional GAN for each class ~ With cGAN implementation by PyTorch ~
Turn an array of strings with a for statement (Python3)
Get the attributes of an object
The story of making a standard driver for db with python.
Check the memory protection of linux kerne with the code for ARM
Touch the object of the neural network
4th night of loop with for
The meaning of ".object" in Django
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
Hit a method of a class instance with the Python Bottle Web API
[Introduction to Python] What is the method of repeating with the continue statement?
I measured the speed of list comprehension, for and while with python2.7.
How to access data with object ['key'] for your own Python class
Try to separate the background and moving object of the video with OpenCV
Explain the mechanism of PEP557 data class
Ask for Pi with the bc command
Align the size of the colorbar with matplotlib
Search for files with the specified extension
Check the existence of the file with python
Change the list in a for statement
[Python3] Rewrite the code object of the function
Pandas of the beginner, by the beginner, for the beginner [Python]
Construction of development environment for Choreonoid class
Array buffer object for use with Cython
Count the number of characters with echo
Extract the color of the object in the image with Mask R-CNN and K-Means clustering
Dynamically add CSV header as attribute name of Model class using setattr of python
I want to add silence to the beginning of a wav file for 1 second
I tried object detection with YOLO v3 (TensorFlow 2.1) on the GPU of windows!
Turn multiple lists with a for statement at the same time in Python
Checklist on how to avoid turning the elements of numpy's array with for
Align the number of samples between classes of data for machine learning with Python
The story of doing deep learning with TPU
Note: Prepare the environment of CmdStanPy with docker
The story of low learning costs for Python
Prepare the execution environment of Python3 with Docker
Recreate the django staff registration screen with class
2016 The University of Tokyo Mathematics Solved with Python
[Note] Export the html of the site with python.
See the behavior of drunkenness with reinforcement learning
Exposing the DCGAN model for Cifar 10 with keras
Increase the font size of the graph with matplotlib
Calculate the total number of combinations with python
Use logger with Python for the time being
Watch out for the return value of __len__
Check the date of the flag duty with Python
I played with Floydhub for the time being
Eliminate the inconveniences of QDock Widget with PySide
Python: Prepare a serializer for the class instance:
Challenge the Tower of Hanoi with recursion + stack
Image processing? The story of starting Python for
Rewrite the name of the namespaced tag with lxml
Fill the browser with the width of Jupyter Notebook
Predict the second round of summer 2016 with scikit-learn