Is An Element of a Sequence an Object?

2017-06-03 Thread Jon Forrest

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?

Cordially,
Jon Forrest



--
https://mail.python.org/mailman/listinfo/python-list


Re: Is An Element of a Sequence an Object?

2017-06-03 Thread Jon Forrest

On 6/3/2017 12:38 PM, Thomas Jollans wrote:

>> 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?
>
> No, strings don't internally store the characters as objects, and yes,
> the slicing operation creates objects. However, strings *are* sequences,
> sequences *are* ordered collections of objects.

I don't see how both can be true. "Object" has a clear meaning in
Python, and the contents of a sequence don't meet the requirements,
as I understand them.

If there were some way of measuring the number of objects in
a program, then it would be easy to prove (or disprove) my hypothesis.
In other words this program

a = "abc"

would have the same number of objects as this program

a = "abcdefghijklmnopqrstuvwzyz"

> The sentence "A sequence is an ordered collection of objects" means that
> a sequence has (ordered) elements, you can access these elements, and
> when you do that, you will get an object (seeing as everything is an
> object).

The distinction between an "object" and "element" is key here. (This
might be seen as pedantic, but I think it's important to be clear,
especially in a book intended for beginners, as I am.

> Hope this helps.

Thanks for taking the time to reply. I'm somebody who reads technical
books *very* carefully so the distinction between objects and elements
is important to me.

Cordially,
Jon Forrest

--
https://mail.python.org/mailman/listinfo/python-list


Re: Is An Element of a Sequence an Object?

2017-06-03 Thread Jon Forrest

On 6/3/2017 5:03 PM, Ben Finney wrote:

Jon Forrest  writes:


I'm learning about Python. A book I'm reading about it


Can you say which book, and where in the book it says this?


With all due respect, I'd rather not. The author has been very
responsive when I raised this issue, and I don't want to make
him look bad.


To phrase it that way might be slightly misleading, though. It is *not*
true, for example, to say that the characters in the string already
exist as objects.


That's exactly what I was thinking, and why I raised the issue.


I think, rather, that you may have an incorrect understanding of a
sequence. The objects produced by an operation on a sequence – a slice,
or iterating the sequence – do not necessarily exist as distinct objects
before that operation.


I wasn't thinking so much about the objects produced by an operation
on a sequence as I was about when the sequence is created. A trivial
sequence is

"abc"

As I understand it, this is one object, not three. The original excerpt
implies the later.

Jon


--
https://mail.python.org/mailman/listinfo/python-list


Re: Is An Element of a Sequence an Object?

2017-06-03 Thread Jon Forrest

On 6/3/2017 4:58 PM, Chris Angelico wrote:


A sequence doesn't necessarily "contain" anything.


Maybe not always, but doesn't

"abc"

contain three characters? Is 'contain' the right word?


As has been mentioned, a range object is a sequence, but it creates integer
objects lazily.


So there must be a class of sequence, like a literal string, that
contains multiple characters stored as one object, and another
class, like range(50), that doesn't store anything, and only
produces values when it's iterated over. Plus, a list is a sequence,
but in this case it's clear that each element of a list is an object.

Jon

--
https://mail.python.org/mailman/listinfo/python-list


Re: Is An Element of a Sequence an Object?

2017-06-03 Thread Jon Forrest

On 6/3/2017 5:23 PM, Steve D'Aprano wrote:

On Sun, 4 Jun 2017 05:10 am, Jon Forrest wrote:



We can fix the book's statement by changing it to:

 A sequence is an ordered collection of *elements* ...


That's exactly what I was thinking, but then there'd have to
be a clear definition of "element".

Jon


--
https://mail.python.org/mailman/listinfo/python-list


Question About When Objects Are Destroyed

2017-08-04 Thread Jon Forrest

Consider the following Python shell session (Python 3.6.2, Win64):

>>> def givemetwo():
... x = 'two'
... print(id(x))
...
>>> givemetwo()
1578505988392

So far fine. My understanding of object existence made me
think that the object referred to by x would be deleted when
the givemetwo() function returned, like a local variable in C.

However, this isn't true, as shown by the following in the same
session:

>>> import ctypes
>>> print (ctypes.cast(1578505988392, ctypes.py_object).value)
two

This shows that the object still exists, which was a surprise.

Will this object ever be deleted? I'm learning about function
decorators which, as my early studies tell me, depend on calling
a function defined inside another function. This suggests that
objects defined inside functions are never deleted, otherwise
function decorators would never work. (I'm not 100% sure
my understanding of function decorators is correct since I'm
still learning about them).

What's the right way to think about this?

Cordially,
Jon Forrest

--
https://mail.python.org/mailman/listinfo/python-list


Re: Question About When Objects Are Destroyed

2017-08-04 Thread Jon Forrest

On 8/4/2017 4:34 PM, gst wrote:

'two' is a so called constant or literal value .. (of that
function).

Why not attach it, as a const value/object, to the function itself ?
So that a new string object has not to be created each time the
function is called. Because anyway strings are immutable. So what
would be the point to recreate such object every time the function is
called ?


This was just an example program, not meant to do anything
meaningful. I would think that the same object behavior would
occur if I dynamically created an object in that function.

Jon




--
https://mail.python.org/mailman/listinfo/python-list


Question About When Objects Are Destroyed (continued)

2017-08-04 Thread Jon Forrest

Perhaps the reason the variable isn't destroyed is
shown by the following (again, in the same session):

>>> import sys
>>> sys.getrefcount(1578505988392)
3

So, maybe it's not destroyed because there are still
references to it. But, what are these references?
Will the reference count ever go to zero?

Jon

--
https://mail.python.org/mailman/listinfo/python-list