2016/12/19
我感觉magic method 是python的进阶主题之一
有时候遇到, 感觉自己能看懂, 但是实际上不清楚其执行流程
官网文档看了一遍, 感觉不过瘾, 是否有更好的资料呢?
目的是什么
1. 对magic method做深入了解
2. 起码自己能用上
产品版本: Python2.7.x
操作系统: Linux
magic method: 类似__getattr__
这一类的, python里面很多, 各有不同作用
what is python magic method?
python magic method advanced
PHPz2017-04-18 10:08:32
I don’t recommend learning all dunder names at once.
Because Python’s dunder names are used in countless places: language level, library level, general, used by specific modules/classes, class level, module level. Too many! I suggest you learn by category and understand each category when you meet them. For example, operator overloading, adding behavior by pretending to be a list / dict, descriptor, module import related, class creation, management and its own attributes, iteration and asynchronous iteration, context management, etc.
Mainly look at the code and official documents you have on hand. If you don’t understand anything, just Google it and you’ll basically be fine. If you still have any questions, you can check the source code. Python source code is very well organized.
大家讲道理2017-04-18 10:08:32
I don’t think it is necessary to deliberately understand the magic methods of Python. If you want to advance in Python, you can read the Python source code, some common modules, etc. During the reading process, you will definitely see the use of Python magic methods frequently. See how the source code is used. Maybe you will gain more insights by thinking about why the author uses it this way. If you don’t understand, just go to the search engine and look it up. One family’s opinion, for reference only.
大家讲道理2017-04-18 10:08:32
If you want to have a rough understanding of Python’s magic methods, you can read this English version blog by rafekettler
I read this English blog several times, and also tried to retell it in Chinese and wrote it on my blog. Just think of it as an advertisement for me, but I really think my reprocessing is pretty good.
Of course, even after reading the above article, it can only let you know what these magic methods can do. In practice, you still need to read more excellent source codes and learn how others use them skillfully. Just like your Python code will become more and more Pythonic as you write it, the magic method will feel natural as you use it.