improve this newbie code/nested functions in Python?

Terry Reedy tjreedy at udel.edu
Fri Mar 20 16:57:24 EDT 2009


Esmail wrote:

>> In my opinion,
>> neither should be nested.  Nothing is gained and something is lost.
>> Neither are used by client; indeed both use client.
> 
> I nested them because I see them as components of the client which
> keeps track of the connection parameters and makes the initial
> connection and then hands the info off to the two threads for
> processing, and then also helps the two threads communicate with
> each other.
> 
> This seemed like a good choice to me, can  you (or someone else)
> elaborate why this is not a good design? The idea was to encapsulate
> all the client info/code in one place.

They are all encapsulated in the module.

To make a closure, the inner function *must* be nested in the outer.
To be an instance method, a function *must* be a class attribute, and 
the easier way to indicate that is by nesting.

In this case, the client does *not* use the other two classes, so the 
nesting is misleading.  I think the only time a class *might* be nested 
is when it is used by and only (directly) used by whatever it is nested 
in -- and even then, nesting is not necessary.  A class statement can 
only use global and class local names and not the non-global names in 
the surrounding context.

tjr




More information about the Python-list mailing list