首先,我认为最好有一个语法来表示符号。 在名称之前添加一些特殊字符可能是一个很好的方法来获取实现:$ 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 moreimportant than their actual value. Two symbols needs only to beequally-comparable. Also, symbols need to be hashable to use as keys ofdictionary (symbols are immutable objects).Motivations===========Currently, there is no obvious way to define constants or states orwhatever would be best represented by symbols. Discussions oncomp.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 fileopened/closed/error) and unique objects (i.e. attributes names could berepresented as symbols).Many languages propose symbols or obvious ways to define symbol-likevalues. For examples in common languages:In C/C++ : Symbols are emulated using Enums.In Haskell/OCaml : Symbols are defined by union types with emptyconstructors. Symbols are local to modules.In Prolog : Symbols are called atoms ... they are local to modules (atleast in swi-prolog)In Ruby : Symbols are introduced be the ":" notation. ":open" is asymbol. Symbols are global.In LISP : Symbols are introduced by "''". "''open" is a symbol. Symbolsare 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 toachieve 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 forboth local and global symbols). For example, for the state of the fileobject I would write : file.$open, file.$close, file.$errorThen, given some other objects (say some other device which also may beopened) : assert f.state != dev.statewould always hold if both objects use locally-defined symbols. The onlyway for these states to be equal would be, for example, for the deviceobject to explicitly assign the file symbols : dev.state = file.$openBy default, symbols should be local to the current module. Then, beingin the module "device_manager", this would hold: assert $opened == device_manager.$openedThere should be a way to go from strings to symbols and the other wayaround. For that purpose, I propose: assert symbol("opened") == $opened assert str($opened) == "opened"Implementation==============One possible way to implement symbols is simply with integers resolvedas much as possible at compile time.The End=======Thanks to those who read entirely this proposal and I hope this proposalwill gather enough interests to become a PEP and someday be implemented,maybe (probably?) in a completely different way ;)Pierre 解决方案 open,close, ... areymbols.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 forboth local and global symbols). For example, for the state of the fileobject I would write : file. 这篇关于在Python中添加符号的提议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-28 01:34
查看更多