Is An Element of a Sequence an Object?

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Mon Jun 5 03:38:50 EDT 2017


Peter Otten writes:

> Thomas Jollans wrote:
>
>> On 04/06/17 09:52, Rustom Mody wrote:
>>> On Sunday, June 4, 2017 at 12:45:23 AM UTC+5:30, Jon Forrest wrote:
>>>> I'm learning about Python. A book I'm reading about it
>>>> says "... a string in Python is a sequence. A sequence is an ordered
>>>> collection of objects". This implies that each character in a string
>>>> is itself an object.
>>>>
>>>> This doesn't seem right to me, but since I'm just learning Python
>>>> I questioned the author about this. He gave an example the displays
>>>> the ids of string slices. These ids are all different, but I think
>>>> that's because the slicing operation creates objects.
>>>>
>>>> I'd like to suggest an explanation of what a sequence is
>>>> that doesn't use the word 'object' because an object has
>>>> a specific meaning in Python.
>>>>
>>>> Am I on the right track here?
>>> 
>>> Its a good sign that you are confused
>>> If you were not (feeling) confused, it would mean you are actually more
>>> so… Following is not exactly what you are disturbed by... Still closely
>>> related
>>> 
>>>>>> s="a string"
>>>>>> s[0]
>>> 'a'
>>>>>> s[0][0]
>>> 'a'
>>>>>> s[0][0][0][0][0]
>>> 'a'
>> 
>> Also:
>> 
>>>>> s[0] is s[0][0][0][0][0][0][0]
>> True
>>>>>
>
> However, this is an implementation detail:
>
>>>> def is_cached(c):
> ...     return c[0] is c[0][0]
> ...

I think this works the same, and looks more dramatic to me:

...    return c[0] is c[0]

>>>> is_cached(chr(255))
> True
>>>> is_cached(chr(256))
> False

Also same thing, as far as I can see:

>>> s = "\u00ff\u0100" ; (s[0] is s[0], s[1] is s[1])
(True, False)



More information about the Python-list mailing list