Re: Style question: Nicknames for deeply nested objects

Discussion in 'Python' started by Stephen Hansen, Jan 30, 2011.

  1. On 1/30/11 9:51 AM, Gerald Britton wrote:
    > 1. If you had to choose between approaches 1 and 2, which one would
    > you go for, and why?


    Neither. Ideally, I'd tweak the API around so the deeply nested
    structure isn't something I need to access regularly. But! If you can't
    do that, I'd do something like:

    --- start
    from contextlib import contextmanager

    class Item(object): pass

    deeply = Item()
    deeply.nested = Item()
    deeply.nested.thing = Item()

    @contextmanager
    def my(thing):
    yield thing


    with my(deeply.nested.thing) as o:
    o.hello = 1

    print deeply.nested.thing.hello
    --- end

    That's a dummy context-manager which basically does nothing: it just
    "abuses" the context manager protocol to basically make a local variable
    and indent its usage. Its really just a run-around and slightly less
    efficient to do:

    > _o = some.deeply.nested.object
    > _o.method(_o.value)


    But with the whitespace added on.

    Personally, I'd usually just make a local variable and skip any tricks.

    --

    Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.10 (Darwin)

    iQEcBAEBAgAGBQJNRayRAAoJEKcbwptVWx/l9X8H/R/WbT6dPuF5eeLH7rbAa/yN
    tq1S+h3JNi6ABrh4pY9qqg0xVT76L269szGNOZPqqDnkqYPLi8bYeJLHqQYx0jZS
    v5bPCpuIqTMnzKuXs3nN1ZKI3fJ0hAdSGuzDxmXWXGsJIR/lkl2tU0+cGfVjHdng
    98jz8Tq00dHZhKD67351BYZVNojDXVFmLZ5soZd5L1ZTg2Q6aD98HbeZvzyWr9fC
    iiK6mwcQHxAwqHoTfOOOr5RZ1kjtoE5nF0cdsqgTbrcAzj1LsXwUX7IN/W922Xm1
    zxNYtwHX1NW4AZooO918Xr0goMv8mxb5n5jeLzceIFg1cOpMiknkH2Ec604OsDo=
    =uJIj
    -----END PGP SIGNATURE-----
    Stephen Hansen, Jan 30, 2011
    #1
    1. Advertising

  2. Stephen Hansen

    rantingrick Guest

    On Jan 30, 12:23 pm, Stephen Hansen <me+list/> wrote:

    > --- start
    > from contextlib import contextmanager
    >
    > class Item(object): pass
    >
    > deeply = Item()
    > deeply.nested = Item()
    > deeply.nested.thing = Item()
    >
    > @contextmanager
    > def my(thing):
    >     yield thing
    >
    > with my(deeply.nested.thing) as o:
    >     o.hello = 1
    >
    > print deeply.nested.thing.hello
    > --- end


    Well congratulations Stephen, you win the obfuscation prize of the
    year!
    rantingrick, Jan 30, 2011
    #2
    1. Advertising

  3. On 1/30/11 10:35 AM, rantingrick wrote:
    > Well congratulations Stephen, you win the obfuscation prize of the
    > year!


    Yes,

    On 1/30/11 10:09 AM, rantingrick wrote:
    > Here is how a pythonic local block would look
    >
    > with this as localvar:
    > localvar.do_something()


    verses

    with my(this) as localvar:
    localvar.do_something()

    Is dreadfully more, er, obfuscated.

    I mean someone would have to know what the 'my' function does to
    understand what's going on!

    OH MY GOD. How can someone be expected to understand what a function does!

    Be serious! You can't expect that of them.

    --

    Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.10 (Darwin)

    iQEcBAEBAgAGBQJNRbOOAAoJEKcbwptVWx/lFR4H/13sV+9g1Ui4SNWXf9n73u6H
    7o/5/tex2KviW2lpoZm8Y0xG0O3dEI0ttp2XahIUAjC81zXVleQMli65W7VE1FG4
    2vlBE1jqKIWu5+Ry1vFZsyxv5FRx5t5bOs0WBQUoiOj841q7h1hGSZ2bQ49sIW4N
    XFgKf3YkCUGQ0XWPICPqYnsdoUVwMQZ+/jkOnUFapccrSFmUOCqOvdestRa+sfJT
    DD9NYfC43i9FLHtS8lITQQd/VVQEW9B+vVGihQjc+o+poitvzCAJXVm+GP3ZReeo
    bboBJSjwHk8WyRzoER50pIrjbBriz6W8PJ+Av0BjxQMPOjq8/BL/rQ2C7mHOBrs=
    =I6xM
    -----END PGP SIGNATURE-----
    Stephen Hansen, Jan 30, 2011
    #3
  4. Stephen Hansen

    rantingrick Guest

    On Jan 30, 12:53 pm, Stephen Hansen <me+list/> wrote:
    > On 1/30/11 10:35 AM, rantingrick wrote:
    >
    > > Well congratulations Stephen, you win the obfuscation prize of the
    > > year!

    >
    > Yes,
    >
    > On 1/30/11 10:09 AM, rantingrick wrote:
    >
    > > Here is how a pythonic local block would look

    >
    > > with this as localvar:
    > >     localvar.do_something()

    >
    > verses
    >
    > with my(this) as localvar:
    >     localvar.do_something()
    >
    > Is dreadfully more, er, obfuscated.


    Absolutely!

    > I mean someone would have to know what the 'my' function does to
    > understand what's going on!


    Yes, and also how decorators word and generators work, and ...

    > OH MY GOD. How can someone be expected to understand what a function does!


    Yes, and also how decorators word and generators work, and ...

    > Be serious! You can't expect that of them.


    I don't. I don't expect anyone to write 10 lines of obfuscation code
    when just two will suffice. Maybe you should join the perl group as
    they would proud!
    rantingrick, Jan 30, 2011
    #4
  5. On 1/30/11 1:13 PM, rantingrick wrote:
    > On Jan 30, 12:53 pm, Stephen Hansen <me+list/> wrote:
    >> OH MY GOD. How can someone be expected to understand what a function does!

    >
    > Yes, and also how decorators word and generators work, and ...
    >
    >> Be serious! You can't expect that of them.

    >
    > I don't. I don't expect anyone to write 10 lines of obfuscation code
    > when just two will suffice. Maybe you should join the perl group as
    > they would proud!


    Riiight.

    "suffice" doesn't mean what you think it means. Generally, if something
    suffices -- it actually, you know, ... works.

    My four lines of setup can get put into a library and treated as a
    recipe if they don't want to get into understanding generators or
    decorators: then its two lines to use, and those two lines are exactly
    like your two lines except for two little details:

    1. They add a function call to the syntax.
    2. They actually work.

    The OP doesn't have to understand decorators or generators if he doesn't
    want to: though I encourage him to do so, as they are beautiful and
    elegant tools that can very clearly and concisely help solve a lot of
    problems.

    Now, me? I wouldn't use the recipe, as I originally said in my response.
    I'd just use a local variable. But the OP didn't like that, and he
    wanted some indenting and whitespace to clearly demarcate where he
    intended to use the local. So I gave him a way to do that.

    You gave him... uh, what was it again?

    Oh, right.

    Nothing.

    As usual.

    --

    Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.10 (Darwin)

    iQEcBAEBAgAGBQJNRiQUAAoJEKcbwptVWx/lMCIIAKg5YxIN7LNpj2mGldmZQrzO
    fU6CNcm/rNR4C2bkFL4mmf1jpD1FMCZucxUJtyWFwN+tCcnuruFRsDN8lcgr4reY
    8mcxgKYoDP4nyF0VJd4eXss/XH+47PjnluBEuzgvfMRgeLA7zNOB9CyT5yBk6pbF
    kzX3Lpz/XM8yp95xO8daq5TfPq3CEiHaKH708Ej8NLPd90Sm/xTWCox7YUrJQNXY
    vN0qiJPJ8bW9xeYCHv+Q5+TG5B3NOuOo/fcMvM8lGgZQSVj9L44BA/ON2iLphDoe
    uyavA79tYiHt+M16rKgf+6BgEfueLdE9Ev2NVd99psRi1T1hUrGP7Vo4wbz7MXg=
    =irQV
    -----END PGP SIGNATURE-----
    Stephen Hansen, Jan 31, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward C. Jones

    Pickle vs. eval for deeply nested objects

    Edward C. Jones, Feb 18, 2004, in forum: Python
    Replies:
    0
    Views:
    482
    Edward C. Jones
    Feb 18, 2004
  2. Ori Y
    Replies:
    1
    Views:
    401
    Bob Ippolito
    Feb 28, 2004
  3. Kirk Strauser
    Replies:
    1
    Views:
    297
    Kirk Strauser
    Jun 11, 2004
  4. Kay Schluehr
    Replies:
    7
    Views:
    336
    Kay Schluehr
    Jan 20, 2006
  5. Gerald Britton
    Replies:
    3
    Views:
    246
    Steven D'Aprano
    Jan 30, 2011
Loading...

Share This Page