nonlocal fails ?

Michael Torrie torriem at gmail.com
Thu Nov 14 19:11:56 EST 2019


On 11/14/19 2:16 PM, R.Wieser wrote:
> I think I did - though implicitily.   What do normal people use "by 
> reference" arguments for ?   Yep, that is what I wanted too.

Well I've only seen this done in languages where other mechanisms for
returning complex types are not present. For example in C.  Chris talked
about dealing with languages where strings are not first-class citizens,
for example.  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.

In Python there is no similar equivalent, other than hacks involving
passing in mutable objects and using those objects to save the state.
State is maintained either by returning everything from the function you
need to maintain state (in a tuple, for example), or by using OOP.
Another mechanism Python has for doing certain types of state keeping
between calls is the generator idea.

> I'm trying to learn how Python works, and for that I (ofcourse) compare it 
> to other languages.   What-and-how it does something different (or equal!) 
> in regard to them.

Fair enough, but trying to do 1:1 transliterations isn't going to help
you learn idiomatic Python.


> 1) Have value
> 2) use value in procedure 1
> 3) use updated value in procedure 2
> 4) use again updated value in procedure 1, 2 or maybe 3

A clear case for using an object and placing your procedures as methods
to the object.  Everything in Python is OO under the hood, even if you
are not forced to use a particular paradigm.  Even a module is an
object.  It's a sort of singleton really.  functions are like a static
method in other languages.  The beauty of Python's organization, though,
is you don't have to use a forced class structure as your namespace.
Modules and packages provide a nicer namespace than Java or C#'s method.
 But I digress.

> For the sake of learning I'm now going over all of the possibilities to see 
> if and how they work.  For that I've already excluded globals and the 
> practice of feeding the value as an argument to the procedure and than store 
> its result as the new one.  I've also excluded using a class object and put 
> the code in there, as well as faking a "by reference" passing by using a 
> tuple.   "nonlocal" looked to be another option, but it appears to be rather> limited in its application.

Well be sure to add Python's classes and objects to your list of
possibilities.  And also explore generators (coroutines).


> In short, the /ways to the goal/ are (currently) important to me, not the 
> goal itself (I'm sure that apart from the "nonlocal" one all of the above 
> are viable, and I thus can get /something/ to work)
> 
> Yeah, I'm weird like that.  Sorry.

Okay I just was wondering if there is a particular task at hand.  Often
that's the best way to learn a language. You have a specific task you
want to accomplish, or program you need to write, and then find out the
best way to do it within the abilities and strengths of the language.

Years ago I was taught Prolog and man I fought that language because I
never really grasped the concepts and wanted to program in C or Pascal
instead of Prolog.  If I knew then what I know now I would have probably
had an easier time of it.


More information about the Python-list mailing list