What is a function parameter =[] for?

Chris Angelico rosuav at gmail.com
Thu Nov 19 14:58:42 EST 2015


On Fri, Nov 20, 2015 at 6:48 AM, BartC <bc at freeuk.com> wrote:
> On 19/11/2015 19:09, Chris Angelico wrote:
>>
>> On Fri, Nov 20, 2015 at 5:50 AM, BartC <bc at freeuk.com> wrote:
>>>
>>> But you're not going to tell me what it is I got wrong!
>>>
>>> I said that Python's "=" does a very shallow copy. And I stated that in
>>> A=B,
>>> something of B must be copied into A.
>>>
>>> I (and probably others) would like to know why none of that is correct.
>>> But
>>> I suspect I'm not wrong.
>>
>>
>> There's no copying happening. You evaluate the expression `B`, and get
>> back some kind of object (because all expressions in Python evaluate
>> to objects, unless they raise exceptions or in some way don't finish
>> evaluating). The name A then becomes bound to that object. You're not
>> copying a reference; you're simply referencing the result of an
>> expression.
>
>
> OK, so it's just a coincidence that after A=B, id(A) appears to be an
> identical copy of id(B)? And which also agrees with my assertions that a
> very shallow copy is done, and that some aspect of B is duplicated in A.

When you execute A=B, you start by evaluating the expression B. It's
not a coincidence that re-evaluating a simple name immediately
afterward yields the same object:

>>> id(B)
140330420093056
>>> id(B)
140330420093056
>>> id(B)
140330420093056
>>> id(B)
140330420093056

But that's all you've proven. You've shown that the result of
evaluating B twice was the same object each time. That's no proof of
copying; it's just proof that, barring threading or other reentrancy
problems, expressions consisting solely of simple names are stable.

>> It is an excellent explanation of the exact points you're confused about.
>
>
> As far I'm concerned I'm not confused by these copying aspects. I said 'very
> shallow copy', not 'shallow copy' nor 'deep copy' nor just 'copy'. To me,
> 'very shallow copy' about sums it up.
>
> While Python byte-code for A=B:
>
>     6 LOAD_GLOBAL              0 (b)
>     9 STORE_GLOBAL             1 (a)

Technically that's CPython byte code. Python (the language) specifies
the semantics of assignment, but not the exact byte code used.

> doesn't really disagree with me either as it looks remarkably like any other
> basic copy operation of any machine language or byte-code that has ever been
> invented.

I think you're warping the word "copy" to mean what Python does on
assignment, rather than noting an actual similarity between Python
assignment and any other concept of copying. Remember, Python does not
have pointers, in the sense of machine integers storing addresses.
When you assign one C pointer variable to another, yes, you're copying
that integer value; but Python doesn't work that way, so there is
nothing to copy.

ChrisA



More information about the Python-list mailing list