问题描述
背景 :对于不熟悉它的人,(和TextMate)通过 .tmLanguage
语言定义文件定义的范围提供语法高亮显示和其他功能,基本上是一堆用于识别给定语言的各种构造的正则表达式,例如函数定义,各种类型的字符串,保留字等。
Background: For those not familiar with it, Sublime Text (and TextMate) provides syntax highlighting and other features through scopes which are defined by .tmLanguage
language definition files, basically a bunch of regexes to identify various constructs in a given language, such as function definitions, various types of strings, reserved words, etc.
我是包(可通过(如果您有兴趣),旨在为Python提供更好的语言定义。您可以根据需要在GitHub上阅读有关它的信息,但其中的主要功能之一就是它实际上得到了维护,这与许多Sublime语言多年来没有改变或更新不同。
I'm the maintainer of the Python Improved package (available via Package Control if you're interested) that aims to be a better language definition for Python. You can read about it at GitHub if you want, but one of the key features is that it's actually maintained, unlike many of the Sublime languages that haven't been changed or updated in years.
问题 :最近,我一直在关注双重强调的 __ magic __
东西,之后找到Rafe的关于魔术函数的Kettler,我能够扩展语言定义的这一部分。但是,我没有找到很多内置的魔术变量名称或魔术属性的运气,例如 __ class __
或 __ doc__
。我已经阅读了文档的部分,但是就我的目的而言,这有点不足,并且似乎主要集中在魔术方法名称上。
The question: I've been focusing recently on double-underscored __magic__
stuff, and after finding this excellent treatise by Rafe Kettler on magic functions I was able to expand that part of the language definition quite a bit. However, I've had a bit less luck on finding a good list of built-in magic variable names, or magic attributes, like __class__
or __doc__
. I've gone through the Data Model section of the docs, but it leaves a little bit to be desired for my purposes, and seems to focus mainly on magic method names.
所以我的问题是, support.variable.magic.python
范围应包括什么?到目前为止,这是它的定义:
So my question is, what should be included in the support.variable.magic.python
scope? This is its definition so far:
\b__(all|bases|class|debug|dict|doc|file|members|metaclass|methods|module|name|slots|weakref)__\b
我开始这个项目的原因之一是要向自己学习更多关于Python的知识,到目前为止,我当然已经取得了成功,但是在这一点上,我有点受阻。
One of the reasons I started this project was to teach myself more about Python, and I've definitely been succeeding so far, but I'm kind of stuck at this point.
请明确一点,我不是在寻找喜欢的场外资源(尽管如果您有方便的链接,我会很感激),并且我也不想尝试进行有目的的讨论。我要弄清楚的是,此列表看起来是否合理,还是存在明显的错误。如果您想做自己的事,请,然后
Just to be clear, I'm not looking for a favorite off-site resource (although if you have a handy link I'd appreciate it) and I'm not trying to start an opinionated discussion. All I'm trying to figure out is if this list looks reasonable as-is, or if there are any glaring errors. If you do want to be opinionated, open an issue and I'd be more than happy to discuss.
谢谢!
推荐答案
A,数据模型文档是我能想到的最完整的东西,它甚至没有真正设计成索引。不过,我不清楚您要寻找什么; __ all __
是全局模块, __ slots __
是类属性, __ weakref __
仅显示为插槽列表中的字符串,而 __ module __
是一个函数属性等。
Alas, the Data Model document is the most complete thing I can think of, and it's not even really designed as an index. I'm not entirely clear on what you're looking for, though; __all__
is a module global, __slots__
is a class attribute, __weakref__
only appears as a string inside the slot list, and __module__
is a function attribute et al. I guess any special attribute that's not typically callable, then?
当然,您总是可以问Python。
Of course, you can always ask Python.
>>> dir(type)
['__abstractmethods__', '__base__', '__bases__', '__basicsize__', '__call__', '__class__', '__delattr__', '__dict__', '__dictoffset__', '__dir__', '__doc__', '__eq__', '__flags__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__instancecheck__', '__itemsize__', '__le__', '__lt__', '__module__', '__mro__', '__name__', '__ne__', '__new__', '__prepare__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasscheck__', '__subclasses__', '__subclasshook__', '__weakrefoffset__', 'mro']
>>> import sys
>>> dir(type(sys))
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
乍一看,您肯定会错过 __ mro __
和 __ subclasses __
。使这一点有些复杂的是,有一些特殊的方法仅由恰好内置在Python中的代码使用,而不是由核心语言使用:示例包括 __ format __
(由<$使用c $ c> str.format )和各种ABC方法。
At a glance you're definitely missing __mro__
and __subclasses__
. Complicating this somewhat is that there are some special methods only used by code that happens to be built into Python, rather than by the core language: examples include __format__
(used by str.format
) and the various ABC methods.
我什至都不知道 __ weakrefoffset__
I don't even know what __weakrefoffset__
is.
请注意,Python 3有一些新功能:其中有一个元类诡计使用的__prepare __
方法,函数和方法现在使用魔术名称作为其属性,而不是像 im_self
这样的杂音(请参见用户定义的函数 ,然后在模块和类上都有一个 __ qualname __
。
Note that Python 3 has a handful of new things: there's a __prepare__
method used by metaclass shenanigans, functions and methods now use magic names for their attributes rather than noise like im_self
(see the "User-defined functions" section of Data Model), and there's a __qualname__
on both modules and classes.
此外,完全提到了模块加载程序应该执行的操作,包括设置一些魔术属性: __ name __
, __ file __
, __ path __
, __ loader __
和 __ package __
。
Also, the importing PEP mentions exactly what a module loader should do, including set some magic attributes: __name__
, __file__
, __path__
, __loader__
, and __package__
.
这篇关于内置魔术变量名称/属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!