Who is your daddy: Can I find what object instantiates another object?

Jean-Paul Calderone exarkun at divmod.com
Mon Apr 13 13:10:46 EDT 2009


On Mon, 13 Apr 2009 09:53:04 -0700 (PDT), hubritic <colinlandrum at gmail.com> wrote:
>I want to build a parser object that handles two different log file
>formats.  I have an object that handles Connection logs and an object
>for Filter logs.  Each will instantiate a Parser object, passing in
>the path to individual log files.

If you have two different things to do, perhaps you should consider providing
two different APIs to do them.

>
>There are a number of ways I could figure out whether I am dealing
>with connection or filter log.
>
>I could pass it in when creating the Parser, but that doesn't seem
>very pythonic.

Guessing isn't very Pythonic.

>
>I could figure out the type of log file a particular instance of the
>Parser is working with by looking at the format of the file. This
>wouldn't be hard but it seems unnecessary.
>
>It would be jiffy if I could find what kind of object instantiated
>that particular parser. So if a FilterLog object instantiated it, the
>parser would know to parse a Filter log.

Make a FilterLogParser and a ConnectionLogParser, or make a Parser which
takes a parameter that tells it if it is parsing filter logs or
connection logs.

>
>Can the Parser object know who its Daddy is?
>

Inspecting the call stack or the execution context to decide what to do is
a really awful idea.  It's complicated to implement, complicated to maintain,
complicated to read, and there's no reason to do it.  Just do the simple
thing - add a parameter or make two separate APIs.

Jean-Paul



More information about the Python-list mailing list