Python中SymbolType有什么用?
这是 SymbolType 包,它将符号添加到 Python 中。那些对 Lisp/Scheme 做过任何有用的事情的人可以告诉我如何在 Python 中利用这种类型吗?
它可以用来将来自外部(来自网络)的字符串与内部代码隔离吗?
$ sudo easy_install SymbolType
$ ipython
不幸的是,您不能使用符号将值作为 kwargs 传递:
In [7]: X = s('X', __name__)
In [9]: a = {X: 10}
In [12]: Y = s('Y', __name__)
In [13]: a.update({Y: 20})
In [14]: a
Out[14]: {X: 10, Y: 20}
In [15]: for (k, v) in a.items():
....: print k, v
Y 20
X 10
In [16]: def z(X=0,Y=1):
....: print X, Y
....:
In [17]: z(**a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
...<ipython console> in <module>()
TypeError: z() keywords must be strings
Here's the SymbolType package that adds symbols to Python. Can those who have done anything useful with Lisp/Scheme tell me what how can I take advantage of this type in Python?
Can it be used to isolate strings coming from outside (from the web) from internal code?
$ sudo easy_install SymbolType
$ ipython
Unfortunately, you can't use symbols to pass values as kwargs:
In [7]: X = s('X', __name__)
In [9]: a = {X: 10}
In [12]: Y = s('Y', __name__)
In [13]: a.update({Y: 20})
In [14]: a
Out[14]: {X: 10, Y: 20}
In [15]: for (k, v) in a.items():
....: print k, v
Y 20
X 10
In [16]: def z(X=0,Y=1):
....: print X, Y
....:
In [17]: z(**a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
...<ipython console> in <module>()
TypeError: z() keywords must be strings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
符号不能替代字符串。虽然两者都由
字符序列,在以下情况下不应使用符号代替字符串
这是主导财产。符号代表独特的身份。
这意味着指针相等
(而不是内容相等)可以用来比较它们。
因此,符号经常被用作常量、枚举元素、哈希表键
或标识符(在 lisp 中)。
在 Lisp 中,第一次引用一个符号时,会在
符号表已创建。如果此后引用相同的符号,
它将被查找并代表相同的值。
我不是 Python 程序员,但由于符号没有内置于
对于Python语言来说,它们的使用可能会受到限制或者不是很方便。
您不应该使用它们来隔离字符串。为此使用包装类。
编辑:通读 SymbolType 文档,我发现它们捆绑了原始的
命名空间(通过
__name__
)。 Common Lisp 也这样做。人们也许可以使用这个以某种方式提供设施。
另请阅读此问题。
Symbols are not a replacement for strings. While both are represented by a
sequence of characters, a symbol shouldn't be used in place of a string when
this is the dominant property. Symbols represent a unique identity.
This means that pointer equality
(instead of content equality) can be used to compare them.
For this reason, symbols are often used as constants, enumeration elements, hash table keys
or identifiers (in lisp).
In Lisp, the first time you reference a symbol, a corresponding entry in the
symbol table is created. If the same symbol is referenced thereafter,
it will be looked up and represent the same value.
I'm not a Python programmer, but since symbols are not built into
the Python language, their use may be limited or not very convenient.
You shouldn't use them for isolating strings. Use a wrapper class for that.
Edit: reading through the SymbolType docs, i saw that they bundle the originating
namespace (through
__name__
). Common Lisp does that, too. One might be able to use thisfacility in some fashion.
Also read this question.