[Tutor] beginning to code

bartc bc at freeuk.com
Fri Sep 22 06:48:39 EDT 2017


On 22/09/2017 10:23, Marko Rauhamaa wrote:
> Bill <BILL_NOSPAM at whoknows.net>:
> 
>> I figure that, internally, an address, a pointer, is being passed by
>> value to implement pass by reference. Why do you say "they are right"
>> above? Are you saying it's not pass by reference?
> 
> "Pass by reference" could be "pass by reference to object" (Python,
> Java, JavaScript, Lisp) or "pass by reference to memory slot" (available
> to Pascal and C++).
> 
> Memory slots (or lvalues, as they are known in C) are not first class
> objects in Python, which makes "pass by reference to memory slot" a bit
> tricky in Python. Python *could* add memory slots to its sanctioned
> collection of object types, and it *could* add special syntax to express
> a memory slot reference and dereference ("&" and "*" in C).
> 
> However, Python doesn't need any language changes to implement memory
> slots. A memory slot could be defined as any object that implements
> "get()" and "set(value)" methods:

I didn't understand your examples.

Can Python be used to write, say, a swap() function that works with any 
argument types (not just classes or lists)? Example:

     def swap(&a,&b):        # made up syntax
         a, b = b, a

     x=10
     y="Z"
     swap(x,y)
     print (x,y)             # "Z" and "10"

If not, then it doesn't have reference passing as it is normally understood.

A simpler example:

     def set(&a,value):
        a = value

     set(x,1000000)

Here the type of x is irrelevant anyway. It doesn't even need to be 
initialised to anything first (that might violate something in Python, I 
don't know what).

-- 
bartc



More information about the Python-list mailing list