(Personal notes) Python metaclasses and metaprogramming

A class (or class declaration) is a first-class citizen

reference:

Declaring a class is equivalent to creating an instance of the type class.

 class C: ... ≡ C = type('C', ...)

Customize class generation

reference:

It is the metaclass specification that customizes this type instantiation. You can customize class generation when declaring a class by declaring the metaclass M as a subclass of type and overriding the __new__ method:

class C(metaclass=M): ...

We speculate that it is not possible to force M to be applied when all classes are generated [investigation required].

Customization of instantiation

reference:

Customization of instantiation is done by defining (overriding) the classes __new__ and __init__. In particular, by customizing __new__, it is possible to generate and return a class instance other than the class intended to be generated (C of c = C (...)). This can be used, for example, to implement factory classes. On the other hand, as you know, __init__ is used to initialize the created instance (set properties, etc.).

Uninvestigated: __prepare__

I will investigate this time.

Recommended Posts

(Personal notes) Python metaclasses and metaprogramming
python personal notes
missingintegers python personal notes
[Personal notes] Python, Django
Notes on Python and dictionary types
Personal notes for python image processing
Python Pandas Data Preprocessing Personal Notes
Notes using cChardet and python3-chardet in Python 3.3.1.
WEB scraping with Python (for personal notes)
Notes on building Python and pyenv on Mac
Personal notes and links about machine learning ① (Machine learning)
(Python) HTML reading and regular expression notes
Talking about Python class attributes and metaclasses
Python scraping notes
Metaprogramming in Python
Python study notes _000
Python learning notes
Python beginner notes
Python study notes_006
python C ++ notes
Python study notes _005
Python grammar notes
Python Library notes
Python personal Q.A
python pandas notes
Python study notes_001
python learning notes
Python3.4 installation notes
Notes on installing Python3 and using pip on Windows7
Personal notes to doc Python code in Sphinx
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Pandas Personal Notes Summary
Implemented List and Bool in Python and SQLite3 (personal note)
Batch design and python
Notes on HDR and RAW image processing with Python
Python packages and modules
Vue-Cli and Python integration
Manipulate excel files from python with xlrd (personal notes)
Ruby, Python and map
Python memorandum (personal bookmark)
python decorator usage notes
python input and output
Python and Ruby split
Python Pickle format notes
[Python] pytest-mock Usage notes
First Python miscellaneous notes
Matlab => Python migration notes
Notes around Python3 assignments
Personal notes about the integration of vscode and anaconda
Notes using Python subprocesses
Python3, venv and Ansible
Python asyncio and ContextVar
Python try / except notes
Python framework bottle notes
How to do Bulk Update with PyMySQL and notes [Python]
Notes on deploying pyenv with Homebrew and managing Python versions
[Python] Notes on while statements (writing style and infinite loop)
Notes on reading and writing float32 TIFF images in python
Python notes using perl-ternary operator