Style question: Nicknames for deeply nested objects

Roy Smith roy at panix.com
Sun Jan 30 13:15:05 EST 2011


In article <mailman.1469.1296409883.6505.python-list at python.org>,
 Gerald Britton <gerald.britton at gmail.com> wrote:

> 1. You need to call this thing many times with different arguments, so
> you wind up with:
> 
>    x = some.deeply.nested.object.method(some.other.deeply.nested.object.value1)
>    y = some.deeply.nested.object.method(some.other.deeply.nested.object.value2)
>    z = some.deeply.nested.object.method(some.other.deeply.nested.object.value3)

I would probably turn that into:

    object = some.deeply.nested.object
    object.method(object.value1)
    object.method(object.value2)
    object.method(object.value3)

i.e. make the temporary variable have the exact same name as the last 
component of the deeply nested thing you're trying to refactor.  If the 
scope of use is small and the meaning is obvious from context, sometimes 
I'll shorten the name, i.e.

     obj = some.deeply.nested.object

or even

     o = some.deeply.nested.object

but I tend to avoid doing that.  I'd rather be a little more verbose in 
preference to being a little more cryptic.



More information about the Python-list mailing list