using inspect

Discussion in 'Python' started by Ethan Furman, Nov 12, 2009.

  1. Ethan Furman

    Ethan Furman Guest

    Greetings!

    How wise is it to base code on inspect? Specifically on things like
    live frames on the stack and whatnot. It occurs to me that this is
    leaning towards implementation details, and away from pure, pristine Python.

    As an example, I have this routine in a module I'm working on:

    def _get_module():
    "get the calling module -- should be the config'ed module"
    target = os.path.splitext(inspect.stack()[2][1])[0]
    target = __import__(target)
    return target

    How brittle is this technique?

    ~Ethan~
     
    Ethan Furman, Nov 12, 2009
    #1
    1. Advertising

  2. On Wed, 11 Nov 2009 18:15:22 -0800, Ethan Furman wrote:

    > Greetings!
    >
    > How wise is it to base code on inspect? Specifically on things like
    > live frames on the stack and whatnot. It occurs to me that this is
    > leaning towards implementation details, and away from pure, pristine
    > Python.
    >
    > As an example, I have this routine in a module I'm working on:
    >
    > def _get_module():
    > "get the calling module -- should be the config'ed module" target =
    > os.path.splitext(inspect.stack()[2][1])[0]
    > target = __import__(target)
    > return target
    >
    > How brittle is this technique?



    Download and install PyPy, CLPython, IronPython and Jython and see how
    many it breaks on :)


    I think it's a brittle technique because it is creating a dependency
    where there shouldn't be one. What happens if somebody runs your module
    directly? It might not need to do anything sensible, but it shouldn't
    fail merely because there's no calling module.

    But I don't think it's inherently dangerous. It's a bit hacky, well,
    probably a lot hacky, but it doesn't seem to use anything specifically
    documented as implementation specific:

    http://docs.python.org/library/inspect.html#inspect.stack




    --
    Steven
     
    Steven D'Aprano, Nov 12, 2009
    #2
    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. =?Utf-8?B?QnJpYW4gS2l0dA==?=

    How do you inspect HTML controls in compiled C#?

    =?Utf-8?B?QnJpYW4gS2l0dA==?=, May 22, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    528
    Kevin Spencer
    May 30, 2005
  2. John C.

    inspect aspx form with reflection?

    John C., Dec 5, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    439
    John C.
    Dec 5, 2005
  3. Benjamin Rutt
    Replies:
    4
    Views:
    404
    Mike Meyer
    Jul 8, 2005
  4. Chris Pax

    using inspect on pygtk

    Chris Pax, Sep 29, 2007, in forum: Python
    Replies:
    4
    Views:
    339
    Chris Pax
    Oct 1, 2007
  5. deluxstar
    Replies:
    5
    Views:
    890
    Terry Reedy
    Sep 25, 2010
Loading...

Share This Page