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:27:06 EDT 2016
On Tue, 15 Mar 2016 07:31 am, BartC wrote:
> But how do you pass something that refers to a itself?
You can't. "Names" are not first-class values in Python.
You can pass a string which represents a name, and a namespace, but you
cannot pass just an unquoted name and have Python automatically resolve it
as a writable reference to a name in a namespace.
I'm only aware of a handful of languages which support this sort of
reference semantics: Pascal (of course!), Algol uses the similar "pass by
name" semantics using thunks, C++, and Visual Basic.
> There are good reasons for wanting to do so. Try writing this function
> in Python:
>
> def swap(a,b):
> b,a = a,b
You can't. But you don't need to. The idiomatic way to swap two values in
Python is:
a, b = b, a
This works for any number of values:
a, b, c, d = d, c, a, b
and it even works (with care) for references other than bare names:
a[0], a[1] = a[1], a[0]
--
Steven
More information about the Python-list
mailing list