欲对Python的magic method 做深入了解, 哪些资料比较好( 官方文档以及一些网上文章都感觉没有讲透 )
高洛峰
高洛峰 2017-04-18 10:06:32
0
4
746

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (4)
PHPzhong

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.

    大家讲道理

    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.

      大家讲道理

      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.

        迷茫

        A Guide to Python’s Magic Methods

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!