scope of function parameters
Peter Otten
__peter__ at web.de
Mon May 30 05:41:17 EDT 2011
Laurent Claessens wrote:
> Le 30/05/2011 11:02, Terry Reedy a écrit :
>> On 5/30/2011 3:38 AM, Laurent wrote:
>>
>>> Cool. I was thinking that "5" was the name, but
>>> >>> 5.__add__(6)
>>> File "<stdin>", line 1
>>> 5.__add__(6)
>>
>>
>> Try 5 .__add__(6)
>
> What is the rationale behind the fact to add a space between "5" and
> ".__add__" ?
> Why does it work ?
It's a hint for the tokenizer.
$ cat show_tokens.py
import sys
from tokenize import generate_tokens
from cStringIO import StringIO
from token import tok_name
_name_width = max(len(name) for name in tok_name.itervalues())
def show_tokens(s):
for token in generate_tokens(StringIO(s).readline):
name = tok_name[token[0]]
value = token[1]
print "%-*s %r" % (_name_width, name, value)
if __name__ == "__main__":
show_tokens(" ".join(sys.argv[1:]))
$ python show_tokens.py 5.__add__
NUMBER '5.'
NAME '__add__'
ENDMARKER ''
$ python show_tokens.py 5 .__add__
NUMBER '5'
OP '.'
NAME '__add__'
ENDMARKER ''
More information about the Python-list
mailing list