[Tutor] Module (.py file) that behaves like package

2018-10-29 Thread Quentin Agren
Hi,

By playing around with importlib I noticed that if you define `__path__ =
[os.path.dirname(__file__)]` in a module named spam.py, then you could `import
spam.spam.spam` or `from spam import spam`. In other words, spam.py behaves
like a package although its loader, if asked, tells that it is not, and
that accordingly you cannot `from . import spam` (in the same file, after
setting `__path__`)

Is this intended behavior? (Maybe I should just get out more...)

Thanks!
Quentin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Module (.py file) that behaves like package

2018-10-29 Thread Alan Gauld via Tutor
On 29/10/2018 14:10, Quentin Agren wrote:

> ... (Maybe I should just get out more...)

That depends on whether you can foresee a use for importlib in
your code. If so then getting to know it isn't a bad thing.
If not, do you really want to be looking back on your 60th
birthday thinking "Did I really spend a week of my
life investigating importlib?"

:-)

FWIW In 20 years of using Python I've never used
importlib in any of my real-world programs...
I'm sure somebody has, but not me. So I'd spend
my time investigating modules I may actually
use, like itertools, functools, threading,
multiprocessing or asyncio... Life's too short!

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Module (.py file) that behaves like package

2018-10-29 Thread Quentin Agren
Hi Alan,


> That depends on whether you can foresee a use for importlib in
> your code.


Slightly disagreeing here. I think reading importlib source is a great way
of understanding how the python import system works  (if you don't want to
read C, that is). I think a learnt a lot and it helped me to understand
some frustrating errors I encountered in my day to day programming, for
example regarding module reloading (e.g. using autoreload in ipython).

I was more wondering if the example I pointed out was really contrived or
if this inconsistency in behavior could be an issue to fix.



>  "Did I really spend a week of my
> life investigating importlib?"
>

To quote Ella Fitzgerald: "It's ain't what you do, it's the wat that you do
it" ;)

FWIW In 20 years of using Python I've never used
> importlib in any of my real-world programs...
> I'm sure somebody has, but not me. So I'd spend
> my time investigating modules I may actually
> use, like itertools, functools, threading,
> multiprocessing or asyncio... Life's too short!
>

Definitely so much to learn...

Thanks!
Quentin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor