some problems for an introductory python test
Hope Rouselle
hrouselle at jevedi.com
Wed Aug 11 08:09:48 EDT 2021
Terry Reedy <tjreedy at udel.edu> writes:
> On 8/10/2021 5:27 PM, Hope Rouselle wrote:
>> Terry Reedy <tjreedy at udel.edu> writes:
>>
>>> On 8/10/2021 9:15 AM, Hope Rouselle wrote:
>>>>>>> 2.__add__(3)
>>>> SyntaxError: invalid syntax
>>>> But then I tried:
>>>>
>>>>>>> (2).__add__(3)
>>>> 5
>>>
>>> Add a space is easier.
>>>>>> 2 .__add__(3)
>>> 5
>>>>>>
>> Hah. That's brilliant! So cool.
>
> Python is a little looser about whitespace than one might expect from
> reading 'normal' code when the result is unambiguous in that it cannot
> really mean anything other than what it does. Two other examples:
>
>>>> if3: print('yes!')
> yes!
That's cool to know too, but I would have expected that. Programming
languages tend to ignore whitespace as much as possible. (But someone
followed-up with more details... I'll get there.) But the next one...
>>>> [0] [0]
> 0
Oh! This almost fooled me. It's just a list containing the integer
zero followed by the index-operator (or however the brackets are called
in this case.)
At first I thought maybe there was an implicit operator between lists
just like it happens with strings.
>>> "a" "," "b"
'a,b'
But which operator would it be? It wasn't concatenation, of course. So
I looked at what type() thought of it. Then I tried changing numbers:
>>> [1] [1]
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
[1] [1]
IndexError: list index out of range
And that's when it hit me. :-) Thanks!
More information about the Python-list
mailing list