Style question: Nicknames for deeply nested objects

rantingrick rantingrick at gmail.com
Sun Jan 30 13:09:33 EST 2011


On Jan 30, 11:51 am, Gerald Britton <gerald.brit... at gmail.com> wrote:

[...]

> that I might confuse with the first.  To make it look better I might do this:
>
>    _o = some.deeply.nested.object
>    _o.method(_o.value)
>
> which is fine, I suppose.

It is very fine. And you "supposed" correctly!


> Then, putting on my company hat, I remembered that, from VBA, you could do this:
>
>    with some.deeply.nested.object
>        .method(.value)
>    end with
>
> I like the structure of this, since the subordinate block can be
> indented, which makes it stand out.  Also, it avoids temporary
> variables.

Yes it is a good idea! Well forgetting the horrendous VBA syntax this
is. I brought this up many moons back as a feature request: Local
Blocks. Here is how a pythonic local block would look

with this as localvar:
    localvar.do_something()


> So, I was thinking of how to get close to this in Python.  I came up
> with two approaches:
>
> 1.
>
>    _o = some.deeply.nested.object
>    if 1:
>        _o.method(_o.value)

bad idea!


> 2.
>
>     for _o in [some.deeply.nested.object]:
>        _o.method(_o.value)

even worse idea!


> I have a couple of questions:
>
> 1. If you had to choose between approaches 1 and 2, which one would
> you go for, and why?

neither! Stick with the original.



More information about the Python-list mailing list