Class initialization from a dictionary, how best?

Bengt Richter bokr at oz.net
Fri Jan 14 18:30:49 EST 2005


On 14 Jan 2005 07:32:06 -0800, "brianobush at gmail.com" <brianobush at gmail.com> wrote:

>Yes, my examle here is a tiny part of a larger more complex issue. My
>application is an DOM XML parser that is reading attributes one at a
you mean like <tag attname="value" att2="v2">blah blah</tag> and you are
grabbing things of interest out of a stream of info you are getting
from call-backs? Or the equivalent?
>time. My class that I am creating is used elsewhere and must have
>certain arguments for those uses to continue working. So, I seem to be
>left with creating an intermediate object. Before, I was simply
Unless the "intermediate object" accumulates information for multiple
Test() instances, why couldn't t= Test() be its own "intermediate object"?

If you are accumulating info for multiple instances before creating them
it is not clear from your description.

>creating an object:
>
>t = Test()
>for attr_name in mapping.keys():
>setattr(t, attr_name, value_from_source)
>
>This I feel was ellegant, efficient and clear. However, what I have now
>works but is not clear.
>BTW, t1 is just for example and was just being printed

What about giving Test some methods to do what you'd like? E.g., a micro-step
in that direction from the above would let you write

    t = Test()
    ...
    t.load_info(infosource)

Then the question becomes what infosource should be, or whether you really
need it at all. IOW, if you are doing infosource.add_info(info_id, info_value)
why couldn't you do t.add_info(info_id, info_value), unless you have to
do t2.add_info(...) alternately with t.add_info(...), and if that's the case,
what is the criterion for choosing t vs t2? Maybe that could be done by something
that automatically manufactures t's as needed in a pool of partially complete t's.

But your real requirements are not clear enough here, so you may get help crossing
a stream, but no one will be able to say you are already on the side of the stream
you want to be later, and there's an easy path without need of crossing twice ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list