Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

hubritic

unread,
Apr 13, 2009, 12:53:04 PM4/13/09
to
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.

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.

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.

Can the Parser object know who its Daddy is?

Thanks

Jean-Paul Calderone

unread,
Apr 13, 2009, 1:10:46 PM4/13/09
to pytho...@python.org
On Mon, 13 Apr 2009 09:53:04 -0700 (PDT), hubritic <colinl...@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

Diez B. Roggisch

unread,
Apr 13, 2009, 1:12:21 PM4/13/09
to
hubritic schrieb:

> 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.
>
> 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.

What makes you believe that?

> 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.
>
> Can the Parser object know who its Daddy is?

Yes, by Daddy telling him so. That's how nature does it, and how you
should do it. Or do you think that because DNA-tests are available to us
we should just put all kids into a big pool and make them find out who
their parents are themselves, once they grew up?

Diez

Aahz

unread,
Apr 13, 2009, 1:54:32 PM4/13/09
to
In article <74ha3lF...@mid.uni-berlin.de>,
Diez B. Roggisch <de...@nospam.web.de> wrote:
>hubritic schrieb:

>>
>> Can the Parser object know who its Daddy is?
>
>Yes, by Daddy telling him so. That's how nature does it, and how you
>should do it. Or do you think that because DNA-tests are available to us
>we should just put all kids into a big pool and make them find out who
>their parents are themselves, once they grew up?

+1 QOTW
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

Why is this newsgroup different from all other newsgroups?

Steven D'Aprano

unread,
Apr 13, 2009, 7:50:35 PM4/13/09
to
On Mon, 13 Apr 2009 19:12:21 +0200, Diez B. Roggisch wrote:

>> Can the Parser object know who its Daddy is?
>
> Yes, by Daddy telling him so. That's how nature does it, and how you
> should do it. Or do you think that because DNA-tests are available to us
> we should just put all kids into a big pool and make them find out who
> their parents are themselves, once they grew up?

Oh, were you raised in a hippie commune in the Sixties too?


*wink*

--
Steven

Aaron Brady

unread,
Apr 13, 2009, 7:56:12 PM4/13/09
to
On Apr 13, 6:50 pm, Steven D'Aprano <st...@REMOVE-THIS-

Close. Australia.

0 new messages