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

I'm wrong or Will we fix the ducks limp?

On 08/06/2016 15:18, Antoon Pardon wrote:
> Op 08-06-16 om 14:34 schreef BartC:
>> So you have partial updates and full updates. A proper reference will
>> be able to do both via the reference. Python can only do a partial
>> update and the reason is that the reference points to the object, not
>> the variable; there is no way to change the variable to link it with
>> another, distinct object.
>> If the object is a list, then that can be modified to any extent, even
>> replacing the contents completely, but it will still be a list. In the
>> case of an int or string, then it's impossible to change. So there are
>> limitations to what can be done.
>> Getting back to Pascal (as I /can/ remember how reference parameters
>> work for integers), assigning to a reference integer parameter in a
>> function will change the caller's version. Python can only emulate
>> that by passing a one-element list or using some such trick. Affecting
>> readability and, likely, performance.
> I don't see why we should determine what a /proper/ reference
> can do, based on what it does in one specific language.

Because there are some things that such references can do that Python 
can't do with its object reference model, not without some difficulty or 
having to write convoluted code.

And it doesn't really depend on the language as the differences are easy 
to demonstrate, provided the language still has something along the 
lines of:

   a = b

to do normal assignment.

(1) Reference parameters

    def fn(&x):


    print (a)    # should produce 1000

I've used "&" here as a device to make the 'x' param work as though it 
was passed by reference. That is, an /additional/ reference to the one 
Python might already use behind the scenes.

(2) Replace (not just modify) a variable's value and type indirectly

   a = 17
   p = &a

   *p = "Dog"
   print a      # should be "Dog"

Here "&" and "*" are used to indicate possible 'reference'/dereference 

With the last example, then this Python code:

   a = 17
   p = a

Might result in the following internal structures:

   0x300:a:[pyref:0x10020]             Variable
   0x400:p:[pyref:0x10020]             Variable
   0x10020:[int: 17]                   Object

With the p = &a version, it would be more like:

   0x300:a:[pyref:0x10020]             Variable
   0x400:p:[pyref:0x10040]             Variable
   0x10020:[int: 17]                   Object
   0x10040:[varref: 0x300]             Object

It's that 0x300 reference to 'a' itself, not just the object linked to 
it, that is what Python can't do.