Importing a class without knowing the module

Franck PEREZ franck.perez at gmail.com
Fri Nov 18 07:07:51 EST 2005


On 11/18/05, Mike Meyer <mwm at mired.org> wrote:
> aleax at mail.comcast.net (Alex Martelli) writes:
> > Mike Meyer <mwm at mired.org> wrote:
> >    ...
> >> >> >> How about adding Foo.__file__ to the serialized data?
> >    ...
> >> >> depends on somewhere on it. You can use the module name if you have it
> >> >> available. If not, deriving the module name from the file name is
> >> >> about the best you can do.
> >> > I disagree with the last sentence.  From a filepath of
> >    ...
> >> You should read the next-to-last sentence, which says to use the
> >> module name if you have it. The last sentence starts "If not" -
> >> meaning you don't have the module name. *That's* the case for which
> >> the file name is about the best you can do.
> > I see!  Thanks for clarifying.  Could you please give me an example of a
> > Foo class which has a Foo.__file__ attribute but not a Foo.__module__
> > attribute?  Sorry, must be some moment of weakness on my mind's part
> > (quite possible, since I am recovering from recent surgery), but I
> > cannot think of a situation where that would be the case (while classes
> > with __module__ and w/o __file__ are the normal situation).  Were there
> > no case in which, given a class, you can learn about its file (by a
> > __file__ attribute) but not about its module (by a __module__
> > attribute), I would of course hope that my inability to parse that
> > sentence of yours, which would under such hypothetical circumstaces be
> > an absurd hypothesis, might be more forgivable.
>
> A classes __module__ attribute doesn't always tell you the name of the
> module - or at least, not a name that would be usefull for the the OPs
> use case. That's the case where you don't have the module name.  The
> reference to a classes __file__ attribute was meant to be to the
> modules __file__ attribute - I'm surprised that no one picked up on
> that. Again, assuming that the module has an __file__ attribute at
> all. Getting the __file__ attribute to a module you don't know the
> name of is a bit tricky, but possible.
>
>     <mike
> --
> Mike Meyer <mwm at mired.org>                      http://www.mired.org/home/mwm/
> Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Thanks for your answers. And btw, sorry for top-posting, I'll be more
careful next time.



More information about the Python-list mailing list