Importing a class without knowing the module
aleax at mail.comcast.net
Fri Nov 18 23:24:02 CET 2005
Mike Meyer <mwm at mired.org> wrote:
> > How do you arrange a module so that its classes' __module__ attributes
> > don't tell you the name of the module "that would be useful", yet the
> > module's __file__ DOES give you information that you can usefully
> > process, heuristically I assume, to infer a module name that IS useful?
> So what module name do you import if C.__module__ is __main__?
In this case, I would strongly suggest that raising an exception upon
the attempt to marshal is better than trying to guess that the file
holding the main script could later be innocuously imported from a
different script, or, alternatively, that the same main script would be
running when a future unmarshaling attempt is done. "In the face of
ambiguity, refuse the temptation to guess" is a sound principle.
> > I just don't see it, which of course doesn't mean it can't be done, with
> > sufficient evil ingenuity. But since you're the one arguing that this
> > case is important enough to be worth dwelling on,
> I'm not dwelling on it, you are. All I did was recommend using the
> module name if you had one, and if not then the file name was your
> best bet. You chose to ignore part of my statement to focus on the
> part you disagreed with, because you thought what I had suggested in
> the first place was a better idea. I pointed out this oversight on
> your part, and you've been dwelling on it ever since.
I don't see the history of this thread the same way as you do,
apparently. I first posted to this thread in a post identified as:
Date: Thu, 17 Nov 2005 17:28:56 -0800
Message-ID: <1h66lub.295q3b19n84anN%aleax at mail.comcast.net>
and later replied to a post of yours identified as:
Date: Thu, 17 Nov 2005 23:27:45 -0500
Message-ID: <86k6f6lgvy.fsf at bhuda.mired.org>
i.e., your post, to which I was replying, was about 3 hours later than
my original one. My original post said:
> BEFORE the text you're commenting on?!), but anyway I think that using
> the class's __module__ rather than __file__ should be what you want.
so it seems incorrect to say that you had "suggested in the first place"
the idea of using __module__; on the contrary, in a previous post of
yours, the first one of yours on this thread, specifically:
Date: Thu, 17 Nov 2005 19:10:39 -0500
Message-ID: <867jb6n7cw.fsf at bhuda.mired.org>
your ENTIRE suggestion -- what you "suggested in the first place" -- was
the entirely wrong:
> How about adding Foo.__file__ to the serialized data?
So, it seems to me that you're the one dwelling on the issue, because
you made the original wrong suggestion, and after I corrected it,
repeated and are now defending the gist of it (though not the obviously
wrong idea of using a non-existent __file__ attribute of the Foo class).
> Frankly, I thought you were brighter than that, have no idea why
> you're acting like this, and frankly don't think you need a tutor. I
> figured this out by fooling around with the interpreter before posting
> in the first place, you certainly know enough to do the same.
"Before posting in the first place" (your post at 19:10 EST) you clearly
had not tested your suggestion (nor had you claimed to, until now). In
any case, if your specific idea is that mucking around with the __file__
attribute of the __main__ module is a reasonable way to solve the
conumdrum of marshaling instances of classes defined in __main__ (which,
as per the first paragraph of this post, I dispute) it would have been
vastly preferable to say so in the first place, rather than expressing
this particular case as just an "If not" (wrt "You can use the module
name if you have it available") -- particularly since you DO have the
module name available for classes defined in __main__, it's just
suspicious and risky to use it in marshaling. Incidentally, pickle DOES
just use the module name in marshaling even in this case -- the opposite
tack to what you suggest, and, in its own special way, almost as wrong,
IMHO (just guessing the other way, in the presence of obvious
ambiguity). Still, pickle does take ONE precaution worth noticing: it
checks that the class it's marshaling is, in fact, the same object
that's available under that class's name from the module it belongs to
-- otherwise (e.g., for classes dynamically defined inside a function),
it raises a PicklingError.
As a general point, refusing to marshal data that strongly smells like
it will be hard or unlikely to unmarshal later is commendable prudence,
and I would recommend this general, prudent approach strongly.
More information about the Python-list