The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

Steven D'Aprano steve at pearwood.info
Mon Mar 14 21:11:25 EDT 2016


On Tue, 15 Mar 2016 01:43 am, BartC wrote:

> On 13/03/2016 09:39, Steven D'Aprano wrote:
>> On Sun, 13 Mar 2016 04:54 am, BartC wrote:
> 
>>> Common sense tells you it is unlikely.
>>
>> Perhaps your common sense is different from other people's common sense.
>> To me, and many other Python programmers, it's common sense that being
>> able to replace functions or methods on the fly is a useful feature worth
>> having. More on this below.
>>
>> Perhaps this is an example of the "Blub Paradox":
> 
> Perhaps it's time to talk about something which many languages have, but
> Python hasn't. Not as far as I know anyway.
> 
> That's references to names (sometimes called pointers). 

Calling them "pointers" is misleading and wrong. What you're referring to
are better known as "reference parameters".

C++ has such "call-by-reference", as does Pascal, using "var" parameters.
Algol uses something similar, but instead of call-by-reference it uses
call-by-name.

Like most modern languages, Python doesn't directly support
call-by-reference, but it is easily emulated with any mutable object. The
easiest is perhaps a single element list:

def f(ref):
    ref[0] = 200

a = [100]
f(a)
print a[0]


but in general, such side-effects are frowned upon, and using a more
functional-style without side-effects is better. Why modify a in place by
magic when you can explicitly assign a new value to a?

Also, you might like to read this:

Does Python pass by reference or value?

http://import-that.dreamwidth.org/1130.html


-- 
Steven




More information about the Python-list mailing list