Python Oddity - 打印保留名称 [英] Python Oddity - print a reserved name

查看:79
本文介绍了Python Oddity - 打印保留名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一点奇怪,''print''是一个保留字......

class thing:
pass

something = thing()
something.print = 3
SyntaxError:无效语法打印东西.__ dict__
{}。 __dict __ [''print''] = 3
打印东西.__ dict__
{''print'':3} print something.print
语法错误:语法无效




看到我不能直接设置something.print属性,但是可以间接设置它来
。这种行为是必要的还是仅仅是IDLE检测到语法错误的一种异常?


问候,


模糊



http:/ /www.voidspace.org.uk/atlantib...thonutils.html

解决方案

Michael Foord写道:

something .__ dict __ [''print''] = 3



或者,稍微更漂亮,使用:


setattr(某事,''print'',3)

看到我不能直接设置something.print属性,但可以间接设置它。这种行为是必要的还是仅仅是IDLE检测语法错误的方式的异常?




不,这就是Python的工作原理。您只能使用直接语法来设置名称为有效Python标识符的
set属性,但间接地您可以使用任何字符串作为属性名称。它没有造成任何伤害和

有时它非常有用。


你可以在Pythonn内部使用dict的任何地方做到这一点。

例如,您可以使用任意关键字参数调用函数

,前提是您使用**语法。


Michael Foord写道:

看到我不能直接设置something.print属性,但可以间接设置它。这种行为是必要的还是仅仅是IDLE检测语法错误的方式的异常?




我会说在运行时检查属性是否属性名字是有效的成本太多

表现,所以它没有完成。另一个很好的例子(在交互式

提示符下,这与空闲无关):

类X:
....传递

.... x = X()
setattr(x,"!@〜%", 42)
dir(x)



[''!@〜%'',''__ doc__'',''__ modle__' ']


:-)

彼得


Michael Foord写道:

这里有一点奇怪,''print''是一个保留字......

类东西:


something = thing()
something.print = 3
SyntaxError:语法无效
打印东西.__ dict__
{}
某事.__ dict __ [''print''] = 3
打印东西.__ dict__
{''print'':3} 打印something.prin语法错误:语法无效



看到我不能直接设置something.print属性,但可以间接设置它。这种行为是必要的还是只是IDLE检测语法错误的方式的异常?



这是必要的。您会发现关键字(那些具有特定

意味着作为语言标记的关键字,例如def和print)不能用作

属性。您可以通过直接操作

__dict__来设置属性的事实是不相关的 - 您会发现您只能间接访问这些属性

这样的属性,因为任何尝试无论如何调用Python解释器,直接这样做都会导致语法错误。


问候

史蒂夫


Here''s a little oddity with ''print'' being a reserved word...

class thing: pass
something = thing()
something.print = 3 SyntaxError: invalid syntax print something.__dict__ {} something.__dict__[''print''] = 3
print something.__dict__ {''print'': 3} print something.print SyntaxError: invalid syntax



See that I can''t set the something.print attribute directly, but can
set it indirectly. Is this behaviour ''necessary'' or just an anomaly of
the way IDLE detects Syntax Errors ?

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantib...thonutils.html

解决方案

Michael Foord wrote:

something.__dict__[''print''] = 3


Or, slightly prettier, use:

setattr(something, ''print'', 3)
See that I can''t set the something.print attribute directly, but can
set it indirectly. Is this behaviour ''necessary'' or just an anomaly of
the way IDLE detects Syntax Errors ?



No, that is simply how Python works. You can only use the direct syntax to
set attributes whose names are valid Python identifiers, but indirectly you
can use any string at all as the attribute name. It doesn''t do any harm and
sometimes it can be extremely useful.

You can do this pretty much anywhere that Pythonn uses a dict internally.
For example you can call functions with arbitrary keyword arguments
provided you use the ** syntax.


Michael Foord wrote:

See that I can''t set the something.print attribute directly, but can
set it indirectly. Is this behaviour ''necessary'' or just an anomaly of
the way IDLE detects Syntax Errors ?



I''d say checking at runtime whether attribute names are valid costs too much
performance, so it''s not done. Another nice example (on the interactive
prompt, this has nothing to do with idle):

class X: .... pass
.... x = X()
setattr(x, "!@~%", 42)
dir(x)


[''!@~%'', ''__doc__'', ''__module__'']

:-)

Peter


Michael Foord wrote:

Here''s a little oddity with ''print'' being a reserved word...

class thing:
pass

something = thing()
something.print = 3
SyntaxError: invalid syntax
print something.__dict__
{}
something.__dict__[''print''] = 3
print something.__dict__
{''print'': 3}
print something.print
SyntaxError: invalid syntax


See that I can''t set the something.print attribute directly, but can
set it indirectly. Is this behaviour ''necessary'' or just an anomaly of
the way IDLE detects Syntax Errors ?


It''s necessary. You will find that keywords (those having a specific
meaning as a language token, such as "def" and "print") can''t be used as
attributes. The fact that you can set the attributes by manipulating the
__dict__ directly isn''t relevant - you will find you can only access
such attributes indirectly, since any attempt to do so directly will
result in a syntax error no matter how the Python interpreter is invoked.

regards
Steve


这篇关于Python Oddity - 打印保留名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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