Is An Element of a Sequence an Object?

Paul Barry paul.james.barry at gmail.com
Sun Jun 4 08:38:00 EDT 2017


Hi folks.

The book being referred to is mine, Head First Python 2d Edition as
published by O'Reilly Media at the end of last year.  Jon has copied me on
the discussion to date, so I've been following along.

Here's a small PDF of the single page from my book (page 24), so that you
can all see my use of the phrase in question in context:
http://paulbarry.itcarlow.ie/p24.pdf - my hope is that this will inform the
discussion further.

Regards.

Paul.




On 4 June 2017 at 13:22, Akira Li <4kir4.1i at gmail.com> wrote:

> Jon Forrest <nobozo at gmail.com> writes:
>
> > I'm learning about Python. A book I'm reading about it
> > says "...
> > a string in Python is a sequence.
>
> correct.
>
> > A sequence is an ordered collection of objects".
>
> correct. https://docs.python.org/3/glossary.html#term-sequence
>
> > This implies that each character in a string
> > is itself an object.
>
> Everything in Python is an object. If *s* is a name that refers to a str
> object then *s[i]* returns i-th Unicode code point from the string as a
> str object of length 1:
>
>   >>> s = "ab"
>   >>> s[0]
>   'a'
>
> It does not matter how *s* is represented internally on a chosen Python
> implementation: *s[0]* may return an existing object or it may create a
> new one on-the-fly.
>
> Here's a perfectly valid sequence of ten squares:
>
>   >>> class Squares:
>   ...     def __getitem__(self, i):
>   ...         if not (-len(self) <= i < len(self)):
>   ...             raise IndexError(i)
>   ...         if i < 0:
>   ...             i += len(self)
>   ...         return i * i
>   ...
>   ...     def __len__(self):
>   ...         return 10
>   >>> s = Squares()
>   >>> s[9]
>   81
>
> All 10 squares are generated on-the-fly (though all int objects already
> exist due to the small int caching on CPython).
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Paul Barry, t: @barrypj <https://twitter.com/barrypj> - w:
http://paulbarry.itcarlow.ie - e: paul.barry at itcarlow.ie
Lecturer, Computer Networking: Institute of Technology, Carlow, Ireland.



More information about the Python-list mailing list