nonlocal fails ?

Chris Angelico rosuav at gmail.com
Fri Nov 15 07:07:49 EST 2019


On Fri, Nov 15, 2019 at 11:01 PM R.Wieser <address at not.available> wrote:
>
> As for "complex types" ?   I don't think a(n ASCII) string can be considered
> any kind of a complex type, but most all languages I know of transfer them
> "by reference" only (though some fake "by value" by using a copy-on-write
> mechanism).

"Pass by value" and "pass by reference" become largely irrelevant when
your strings are immutable objects; so I would say that a large number
of languages have broadly "pass-by-value" semantics for strings,
treating them as first-class objects that are defined by value, not
identity.

> > In C you can either return a pointer to the string (and remember who's
> > responsibility it is to free the memory!), or you can allocate memory
> > yourself and pass the pointer to a function, like strcpy.
>
> Or pass a reference to an empty pointer, have the procedure allocate the
> memory and return the address of it in that pointer.

I don't know why you'd do that rather than simply return the new
pointer, but whatever. It's still a lot clunkier than simply returning
a string, which is what happens in all sane high-level languages.

> > In Python there is no similar equivalent,
>
> It looks like it
>
> > other than hacks involving passing in mutable objects
>
> The only "hack" I see here is to transfer /mutable/ object, instead of a
> non-mutable one.

Yes, Python has no concept of pass-by-reference. Python *always*
passes objects around, not variables. But everything that you could do
with "pass by reference" can be done by passing a mutable object, and
everything that you could do by passing mutable objects around can be
done by passing pointers to data structures, or (equivalently) passing
data structures by reference. There's no real difference.

ChrisA


More information about the Python-list mailing list