在Python中添加符号的提议 [英] Proposal for adding symbols within Python

查看:68
本文介绍了在Python中添加符号的提议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我对此提案的每一点都完全开放

(我还不敢打电话给PEP)。


摘要

========


此提案建议在Python中添加符号。


符号是代码在代码中的表示比实际值更重要的对象。两个符号只需要具有相同的可比性。此外,符号需要可以用作

字典的键(符号是不可变对象)。


动机

= ==========


目前,没有明显的方法来定义常量或状态,或者

无论符号最能代表什么。关于

comp.lang.python的讨论显示了至少六种替换符号的方法。


符号的一些用例是:对象的状态(即一个文件

打开/关闭/错误)和唯一对象(即属性名称可以是

表示为符号)。


许多语言提出符号或明显的方法来定义符号类似的

值。有关常用语言的示例:


在C / C ++中:符号使用枚举进行模拟。

在Haskell / OCaml中:符号由联合类型定义为空

构造函数。符号是模块的本地符号。

在Prolog中:符号被称为原子......它们是模块的本地符号(至少在swi-prolog中是


在Ruby中:符号被引入为:符号。 ":开放"是一个

符号。符号是全球性的。

在LISP中:符号由'引入。 "开放"是一个象征。符号

是本地模块。


建议

========

首先,我认为最好有一个语法来表示符号。

在名称之前添加一些特殊字符可能是一个很好的方法来获取
实现:$ open,$ close,...是$ ymbols。


在符号范围内,我认为它们应该是本地的名称空间

(这一点应该讨论,因为我看到

本地和全局符号的优点和缺点)。例如,对于文件状态

对象我会写:

文件。$ open,file。$ close,file。$ error


然后,给出一些其他对象(比如说其他一些设备也可以打开
) ):

断言f.state!= dev.state

如果两个对象都使用本地定义的符号,则
将始终保持不变。这些状态相等的唯一

方式将是,例如,设备

对象明确指定文件符号:

dev.state = file。$ open


默认情况下,符号应该是当前模块的本地符号。然后,在模块device_manager中,

,这将成立:

断言$ open == device_manager。$ open


应该有一种从字符串到符号的方法,另一种方式是

。为此,我建议:

断言符号(打开)== $打开
断言str($ open)==" opens"




实施

==============


实现符号的一种可能方法是在编译时尽可能多地解析整数




结束

=======


感谢那些完全阅读这个提案的人,我希望这个提议

能够收集到足够的兴趣成为PEP和有一天会被实施,

可能(可能?)以完全不同的方式;)


Pierre

解决方案

open,


close,...


ymbols。


在符号范围内,我认为它们应该是本地名称空间

(这一点应该讨论,因为我看到优点和缺点为

bo本地和全球符号)。例如,对于文件状态

对象我会写:

文件。

Please, note that I am entirely open for every points on this proposal
(which I do not dare yet to call PEP).

Abstract
========

This proposal suggests to add symbols into Python.

Symbols are objects whose representation within the code is more
important than their actual value. Two symbols needs only to be
equally-comparable. Also, symbols need to be hashable to use as keys of
dictionary (symbols are immutable objects).

Motivations
===========

Currently, there is no obvious way to define constants or states or
whatever would be best represented by symbols. Discussions on
comp.lang.python shows at least half a dozen way to replace symbols.

Some use cases for symbols are : state of an object (i.e. for a file
opened/closed/error) and unique objects (i.e. attributes names could be
represented as symbols).

Many languages propose symbols or obvious ways to define symbol-like
values. For examples in common languages:

In C/C++ : Symbols are emulated using Enums.
In Haskell/OCaml : Symbols are defined by union types with empty
constructors. Symbols are local to modules.
In Prolog : Symbols are called atoms ... they are local to modules (at
least in swi-prolog)
In Ruby : Symbols are introduced be the ":" notation. ":open" is a
symbol. Symbols are global.
In LISP : Symbols are introduced by "''". "''open" is a symbol. Symbols
are local to modules.

Proposal
========

First, I think it would be best to have a syntax to represent symbols.
Adding some special char before the name is probably a good way to
achieve that : $open, $close, ... are $ymbols.

On the range of symbols, I think they should be local to name space
(this point should be discussed as I see advantages and drawbacks for
both local and global symbols). For example, for the state of the file
object I would write :

file.$open, file.$close, file.$error
Then, given some other objects (say some other device which also may be
opened) :
assert f.state != dev.state
would always hold if both objects use locally-defined symbols. The only
way for these states to be equal would be, for example, for the device
object to explicitly assign the file symbols :
dev.state = file.$open
By default, symbols should be local to the current module. Then, being
in the module "device_manager", this would hold:
assert $opened == device_manager.$opened
There should be a way to go from strings to symbols and the other way
around. For that purpose, I propose:
assert symbol("opened") == $opened
assert str($opened) == "opened"



Implementation
==============

One possible way to implement symbols is simply with integers resolved
as much as possible at compile time.

The End
=======

Thanks to those who read entirely this proposal and I hope this proposal
will gather enough interests to become a PEP and someday be implemented,
maybe (probably?) in a completely different way ;)

Pierre

解决方案

open,


close, ... are


ymbols.

On the range of symbols, I think they should be local to name space
(this point should be discussed as I see advantages and drawbacks for
both local and global symbols). For example, for the state of the file
object I would write :

file.


这篇关于在Python中添加符号的提议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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