python中的魔术名称 [英] magic names in python

查看:76
本文介绍了python中的魔术名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我最近开始在python中工作的次数比以前在

中的工作要多得多。我发现了一些东西,完全消除了漂亮的粉红色云彩,包围了我以前的蟒蛇经历:魔法名称(我感觉几乎和我发现时一样悲伤

在nethack中吃掉你的奇怪的粉红色蠕虫,更不用说

心灵劫掠者 - 我真的很讨厌它们。)


我想所有的编程语言在某种程度上都有魔术名称

(C系列中的fx类具有必须与类相同的

的构造函数( foo :: foo)而不是foo .__ init__)。


我刚刚在这个主题上使用了一个搜索引擎,我发现没有

全面的列表python中的魔法名称。


所以我的问题:

*是否有一个python中的魔术名称的综合列表(到目前为止我是

知道__init__和__repr__)?

*这些列表是完整的还是可以随着时间的推移添加魔术名称(对于
python" core" ;)?

*魔法名称在不同的python版本中是一样的吗?


我也试过((?)的选定部分)unittest包使用在

Zope中它似乎是我为我的测试创建的功能,用魔法

前缀test是神奇的,其他功能则没有。


因此出现了另一个问题:

*是鼓励使用魔法名称和/或良好编码的一部分

练习。


活得长久繁荣,




-
home: www .pererikstrandberg.se

work: www.incf.org

www.spongswedencare.se

Hi,

I recently started working a lot more in python than I have done in
the past. And I discovered something that totally removed the pretty
pink clouds of beautifulness that had surrounded my previous python
experiences: magic names (I felt almost as sad as when I discovered
the strange pink worms that eat you in nethack, not to mention the
mind flayers - I really hate them).

I guess all programming languages have magic names to some extent
(f.x. classes in the "C-family" have constructors that must have the
same name as the class (foo::foo) instead of foo.__init__).

I just used a search engine a little on this topic and I found no
comprehensive list of magic names in python.

So my questions:
* is there a comprehensive list of magic names in python (so far i
know of __init__ and __repr__)?
* are these lists complete or can magic names be added over time (to
the python "core")?
* are magic names the same in different python versions?

I also tried (selected parts of(?)) the unittest package for use in
Zope and it seemed functions that I created for my test with the magic
prefix "test" were magic, other functions were not.

So another question emerges:
* is the use of magic names encouraged and/or part of good coding
practice.

Live long and prosper,
Per

--

Per Erik Strandberg
home: www.pererikstrandberg.se
work: www.incf.org
also: www.spongswedencare.se

推荐答案

在< 11 ********************** @ p77g2000hsh.googlegroups .com>,per9000写道:
In <11**********************@p77g2000hsh.googlegroups .com>, per9000 wrote:

我刚用了一个关于这个主题的搜索引擎,我发现没有

全面python中的魔术名称列表。
I just used a search engine a little on this topic and I found no
comprehensive list of magic names in python.



它们通常在解释相关

功能的部分文档中提及。例如,在

参考手册中的特殊方法名称中。


当然,参考手册中的所有内容都在
$下。 b $ b`_(下划线)`_在其索引中。在`库引用'的索引`_

你会发现模块使用的更多。


... _`_ (下划线)`: http://docs.python.org /ref/genindex.html#letter-_

... _library reference'的索引: http://docs.python.org/lib/genindex.html

... _特殊方法名称: http://docs.python.org/ref/specialnames.html

They are usually mentioned in parts of the docs where the relevant
functionality is explained. For example in `Special method names`_ in the
reference manual.

And of course all covered in the reference manual are under
`_ (underscore)`_ in its index. In the `library reference''s index`_
you''ll find some more that are used by modules.

... _`_ (underscore)`: http://docs.python.org/ref/genindex.html#letter-_
... _library reference''s index: http://docs.python.org/lib/genindex.html
... _Special method names: http://docs.python.org/ref/specialnames.html


*这些列表是完整的还是可以随着时间的推移添加魔术名称(对于
python" core")?
* are these lists complete or can magic names be added over time (to
the python "core")?



可以随着时间的推移添加魔术名称。例如``with``语句

在Python 2.5中引入了`__enter __()`和`__exit __()`。一些特殊的

名称甚至可能会更改,如文档所说的那样,或者在文档中没有提到


Magic names can be added over time. For example the ``with`` statement
introduced `__enter__()` and `__exit__()` in Python 2.5. And some special
names may even change were the docs say so or if they aren''t mentioned at
all in the docs.


*是不同python版本中相同的魔术名称?
* are magic names the same in different python versions?



是。

Yes.


因此出现了另一个问题:

*是鼓励使用魔法名称和/或编写好部分代码

练习。
So another question emerges:
* is the use of magic names encouraged and/or part of good coding
practice.



使用是什么意思?实现它们以覆盖行为?是的,

这是他们的目的。发明新的魔术名字?当然不是,它们是特殊的,因为一个原因:防止与用户姓名发生冲突。


Ciao,

Marc''BlackJack''Rintsch

What do you mean by "use"? Implement them to override behavior? Yes,
that''s their purpose. Invent new magic names? No of course not, they are
special for a reason: preventing name clashes with the user''s names.

Ciao,
Marc ''BlackJack'' Rintsch


per9000写道:
per9000 wrote:

所以我的问题:

*是否在python中有一个完整的魔术名称列表(到目前为止我已经知道了__init__和__repr__的
)?

*这些列表是完整的还是可以随着时间的推移添加魔法名称(对于
pythoncore)?

*是不同python版本中的魔术名称相同?
So my questions:
* is there a comprehensive list of magic names in python (so far i
know of __init__ and __repr__)?
* are these lists complete or can magic names be added over time (to
the python "core")?
* are magic names the same in different python versions?



我不相信所有__magic__方法的完整列表。

运算符模块有一个相当广泛的函数列表,可以调用

这样的方法,但我知道有些函数被遗漏了。


在我打破这个

消息的时候,我记得很多...


__init__

__new__

__str__

__repr__

__len__

__nonzero__

__hash__

__cmp __(__ eq __,__ _,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
__getitem__

__setitem__

__delitem__

__iter__

__neg__

__not __


还有非魔法命名,但在迭代器/生成器上仍然有点魔术.next()

方法。


- Josiah

I don''t believe that there is a full list of all __magic__ methods. The
operator module has a fairly extensive listing of functions that call
such methods, but I know that some have been left out.

Among those that I remember off the top of my head while typing this
message...

__init__
__new__
__str__
__repr__
__len__
__nonzero__
__hash__
__cmp__ (__eq__, __ne__, __lt__, __gt__, __le__, __ge__)
__getattr__
__setattr__
__delattr__
__getitem__
__setitem__
__delitem__
__iter__
__neg__
__not__

There''s also the not-magic named, but still somewhat magic .next()
method on iterators/generators.

- Josiah


6月4日上午9:11,Marc''BlackJack''Rintsch< bj _... @ gmx.netwrote:
On Jun 4, 9:11 am, Marc ''BlackJack'' Rintsch <bj_...@gmx.netwrote:

在< 1180939436.862957.127 ... @ p77g2000hsh.googlegroups .com> ;, per9000写道:
In <1180939436.862957.127...@p77g2000hsh.googlegroups .com>, per9000 wrote:


[...]


因此出现了另一个问题:

*是鼓励使用魔法名称和/或编写良好代码的一部分

练习。

[...]

So another question emerges:
* is the use of magic names encouraged and/or part of good coding
practice.



使用是什么意思?实现它们以覆盖行为?是的,

这是他们的目的。发明新的魔术名字?当然不是,它们是特殊的,因为一个原因:防止与用户名字冲突。


What do you mean by "use"? Implement them to override behavior? Yes,
that''s their purpose. Invent new magic names? No of course not, they are
special for a reason: preventing name clashes with the user''s names.



[在我的品味:UGLY]

我想到这样的事情:也许我创建了一个有很多文件工作的类
,并且继承了新类型的文件可以<打开并使用了



当这个类的实例死亡时,需要关闭文件

- 也许是现在而不适用于

垃圾收集器。为了方便这一点,我创建了许多函数

,其名称类似于close _ * _ file(f.x.close_indata_file,

close_outdata_file,close_error_file等)。 (与[PyTest |

unittest]和test *名称进行比较。)


如果此类已被继承到其他一些可以使用的类

多个indata文件或许我们想要神奇地关闭所有文件

通过调用此实例中具有名称的所有函数打开

匹配关闭_ * _文件。


我会认为这是解决它的一种丑陋方式。


[根据我的口味:NICER]

我可能会将文件句柄添加到某个列表中(并鼓励继承

类来使用此列表)并关闭列表中的所有内容。我会

不使用魔法通配符名称。


换句话说:我需要去黄色警报,并提防

魔法在python的非核心附加软件包中命名?或者我应该再多花一点时间来获得RTFM吗? (我讨厌RTFM''。)


活得长久繁荣,




- <每个Erik Strandberg

home: www.pererikstrandberg.se

工作: www.incf.org

www.spongswedencare.se

[in my taste: UGLY]
I think of something like this: Perhaps I create a class that works
with a lot of files, and with inheritance new types of files can be
opened and worked with.

When it is time for an instance of this class to die the files need to
be closed - perhaps "right now" and not when it is suitable for the
garbage collector. To facilitate this I create a number of functions
with names like close_*_file (f.x. close_indata_file,
close_outdata_file, close_error_file etc). (compare with [PyTest|
unittest] "test*" names.)

If this class has been inherited to some other class that works with
more than one indata file perhaps we want to magically close all files
that are open by calling all function in this instance that has names
matching "close_*_file".

I would consider this an ugly way of solving it.

[in my taste: NICER]
I''d perhaps add file-handles to some list (and encourage inherited
classes to use this list) and close everything in the list. I would
not use magic wildcard names.

So in other words: Do I need to go to yellow alert, and watch out for
magic names in non-core add-on packages to python? Or should I just
RTFM a little more? (I hate RTFM''ing.)

Live long and prosper,
Per

--

Per Erik Strandberg
home: www.pererikstrandberg.se
work: www.incf.org
also: www.spongswedencare.se


这篇关于python中的魔术名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆