错误:搜索内容不能为空,请输入英文关键词
错误:关键词超出字数限制,请精简
高级检索

Magic Methods, Properties, and Iterators

  • Magnus Lie Hetland,
  • Fabio Nelli

摘要

In Python, some names are spelled in a peculiar manner, with two leading and two trailing underscores. You have already encountered some of these (__future__, for example). This spelling signals that the name has a special significance—you should never invent such names for your own programs. One very prominent set of such names in the language consists of the magic (or special) method names. If your object implements one of these methods, that method will be called under specific circumstances (exactly which will depend on the name) by Python. There is rarely any need to call these methods directly.