osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A question on modification of a list via a function invocation


On Wed, 6 Sep 2017 10:11 pm, Rustom Mody wrote:

> On Wednesday, September 6, 2017 at 5:08:20 PM UTC+5:30, Steve D'Aprano wrote:
>> On Wed, 6 Sep 2017 07:13 pm, Rustom Mody wrote:
>> 
>> 
>> > Can you explain what "id" and "is" without talking of memory?
>> 
>> Yes.
>> 
>> id() returns an abstract ID number which is guaranteed to be an integer, and
>> guaranteed to be distinct for all objects which exist at the same time. When
>> an object ceases to exist, its ID number may be re-used.
>> 
>> `is` compares the two operands for identity. If the two operands are the same
>> object, `is` returns True, if they are distinct objects, `is` returns False.
> 
>>>> a = (1,2)
>>>> b = (1,2)
>>>> a is b
> False
>>>> x = 1
>>>> y = 1
>>>> x is y
> True
> 
> a seems to be as 'same' to b as x is to y
> Python seems to think otherwise
> 
> Evidently your ?same? is not the same as mine??

You are (intentionally, I think) being obtuse. I am confident that you know very
well that "the same" as a general English term has many meanings, including:

- same in identity ("the same man I saw yesterday");
- closely similar ("the curtains are the same colour as the carpets");
- equal ("I gave the children the same number of lollies each");
- unchanged ("his attitude is the same as ever");
- of like kind ("you are exactly the same as your mother");

while "the same object" has specifically the first meaning.

How do you know that two objects are the same object? It isn't because they are
equal, or that they look vaguely the same. "Equal" is not equivalent to "the
same object":

py> a, b = [], []  # they sure look the same...
py> a == b
True
py> a.append(None)  # insert None into a
py> None in b  # but they aren't the same object
False


The only way to find out if two objects are the same object is to ask Python --
and Python is under no obligation to comply with your na?ve intuitions about
objects. As you are well aware, because we've told you many times, Python often
caches objects so that they are reused rather than being recreated from scratch
when needed -- and it is free to do so in implementation and version dependent
ways.



>> > In fact we have got so used to the term 'memory' that it actually seems
>> > strange when someone like Dijkstra grumbles at the anthropomorphism and
>> > asks why its not called 'store'.
>> 
>> And if it were called "store" (grocery store? shoe store? clothes store?)
>> Dijkstra would have grumbled at the metaphor and asked why it wasn't
>> called "memory".
> 
> Memory is an old (middle-English) word.
> Until say 1945 it could be used in sentences like the following
> ?I have memories of walking in the woods with my dad?
> ?Where are the eggs??   ?Oops! Totally slipped my memory? Sorry"
> ?The Dalai Lama has memories of his past lives?

I don't understand why you say "until 1945". You can still use memory in those
ways even today.

> Are you using ?memory? in this kind of way?

Yes.



-- 
Steve
?Cheer up,? they said, ?things could be worse.? So I cheered up, and sure
enough, things got worse.