简体   繁体   English

内置魔术变量名称/属性

[英]Built-in magic variable names/attributes

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. 背景 :对于那些不熟悉它的人, Sublime Text (和TextMate)通过.tmLanguage语言定义文件定义的范围提供语法高亮和其他功能,基本上是一堆用于识别给定语言中各种结构的正则表达式,例如函数定义,各种类型的字符串,保留字等

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. 我是Python改进包的维护者(如果你感兴趣,可以通过Package Control获得),它旨在成为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. 如果需要,您可以在GitHub上阅读它,但其中一个关键特性是它实际上是维护的,这与许多未经过多年更改或更新的Sublime语言不同。

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. 问题 :我最近一直关注双重__magic__内容,在找到Rafe Kettler关于魔术函数的优秀论文之后 ,我能够将语言定义的那一部分扩展到相当多的部分。 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__ . 但是,我找到一个很好的内置魔术变量名称列表,或者像__class____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. 我已经浏览了文档的数据模型部分,但它为我的目的留下了一点点需要,似乎主要关注魔术方法名称。

So my question is, what should be included in the support.variable.magic.python scope? 所以我的问题是, support.variable.magic.python范围应该包括哪些内容? This is its definition so far: 这是它到目前为止的定义:

\b__(all|bases|class|debug|dict|doc|file|members|metaclass|methods|module|name|slots|weakref)__\b

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. 我开始这个项目的原因之一就是自学了更多关于Python的知识,到目前为止我确实取得了成功,但是我有点陷入困境。

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. 如果你确实想要自以为是,请打开一个问题 ,我非常乐意与你讨论。

Thanks! 谢谢!

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. __all__是一个模块全球, __slots__是一个类属性, __weakref__仅显示为槽列表内的字符串, __module__是一个函数属性等。 I guess any special attribute that's not typically callable, then? 我想任何通常不可调用的特殊属性呢?

Of course, you can always ask Python. 当然,你总是可以问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__']

At a glance you're definitely missing __mro__ and __subclasses__ . 一目了然,你肯定错过了__mro____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. 有点复杂的是,有一些特殊的方法只用于碰巧构建在Python中的代码,而不是核心语言:例子包括__format__ (由str.format )和各种ABC方法。

I don't even know what __weakrefoffset__ is . 我甚至不知道__weakrefoffset__ 什么。

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. 请注意,Python 3有一些新东西:元类恶作剧使用了__prepare__方法,函数和方法现在使用魔术名称作为属性,而不是像im_self那样的im_self (参见数据模型的“用户定义函数”部分),并且在模块和类上都有__qualname__

Also, the importing PEP mentions exactly what a module loader should do, including set some magic attributes: __name__ , __file__ , __path__ , __loader__ , and __package__ . 此外, 进口PEP提到完全是一个模块加载器应该做的事情,包括设置一些魔法属性: __name____file____path____loader____package__

import gc

print("\n".join(sorted({attrname for item in gc.get_objects() for attrname in dir(item) if attrname.startswith("__")})))
#>>> __about__
#>>> __abs__
#>>> __abstractmethods__
#>>> __add__
#>>> __all__
#>>> __and__
#>>> __annotations__
#>>> __author__
#>>> __base__
#>>> __bases__
#>>> __basicsize__
#>>> __bool__
#>>> __build_class__
#>>> __builtins__
#>>> __cached__
#>>> __call__
#>>> __cause__
#>>> __ceil__
#>>> __class__
#>>> __closure__
#>>> __code__
#>>> __complex__
#>>> __concat__
#>>> __contains__
#>>> __context__
#>>> __copy__
#>>> __copyright__
#>>> __credits__
#>>> __date__
#>>> __debug__
#>>> __deepcopy__
#>>> __defaults__
#>>> __del__
#>>> __delattr__
#>>> __delete__
#>>> __delitem__
#>>> __dict__
#>>> __dictoffset__
#>>> __dir__
#>>> __displayhook__
#>>> __divmod__
#>>> __doc__
#>>> __enter__
#>>> __eq__
#>>> __excepthook__
#>>> __exit__
#>>> __file__
#>>> __flags__
#>>> __float__
#>>> __floor__
#>>> __floordiv__
#>>> __format__
#>>> __func__
#>>> __ge__
#>>> __get__
#>>> __getattr__
#>>> __getattribute__
#>>> __getitem__
#>>> __getnewargs__
#>>> __getstate__
#>>> __globals__
#>>> __gt__
#>>> __hash__
#>>> __iadd__
#>>> __iand__
#>>> __iconcat__
#>>> __ifloordiv__
#>>> __ilshift__
#>>> __imod__
#>>> __import__
#>>> __imul__
#>>> __index__
#>>> __init__
#>>> __initializing__
#>>> __instancecheck__
#>>> __int__
#>>> __inv__
#>>> __invert__
#>>> __ior__
#>>> __ipow__
#>>> __irshift__
#>>> __isabstractmethod__
#>>> __isub__
#>>> __itemsize__
#>>> __iter__
#>>> __itruediv__
#>>> __ixor__
#>>> __kwdefaults__
#>>> __le__
#>>> __len__
#>>> __loader__
#>>> __lshift__
#>>> __lt__
#>>> __missing__
#>>> __mod__
#>>> __module__
#>>> __mro__
#>>> __mul__
#>>> __name__
#>>> __ne__
#>>> __neg__
#>>> __new__
#>>> __newobj__
#>>> __next__
#>>> __not__
#>>> __objclass__
#>>> __or__
#>>> __package__
#>>> __path__
#>>> __pos__
#>>> __pow__
#>>> __prepare__
#>>> __qualname__
#>>> __radd__
#>>> __rand__
#>>> __rdivmod__
#>>> __reduce__
#>>> __reduce_ex__
#>>> __repr__
#>>> __reversed__
#>>> __rfloordiv__
#>>> __rlshift__
#>>> __rmod__
#>>> __rmul__
#>>> __ror__
#>>> __round__
#>>> __rpow__
#>>> __rrshift__
#>>> __rshift__
#>>> __rsub__
#>>> __rtruediv__
#>>> __rxor__
#>>> __self__
#>>> __set__
#>>> __setattr__
#>>> __setitem__
#>>> __setstate__
#>>> __sizeof__
#>>> __slots__
#>>> __stderr__
#>>> __stdin__
#>>> __stdout__
#>>> __str__
#>>> __sub__
#>>> __subclasscheck__
#>>> __subclasses__
#>>> __subclasshook__
#>>> __suppress_context__
#>>> __traceback__
#>>> __truediv__
#>>> __trunc__
#>>> __version__
#>>> __weakref__
#>>> __weakrefoffset__
#>>> __wrapped__
#>>> __xor__

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM