4 ms·
I'm training people in Python regularly and my classroom experience is: ALWAYS USE __INIT__.PY EVERYWHERE. Don't use this namespace feature. You think you under
by sametmax 7y ago
I'm training people in Python regularly and my classroom experience is: ALWAYS USE __INIT__.PY EVERYWHERE. Don't use this namespace feature. You think you understand how it works, but you don't. It will bite you.
- simonh 7y agoI just read the docs on this, but don't really grok the pros-and-cons. Can you give an example of a gotcha? That would be very helpful.
- sametmax 7y agoNamespaces are useful for library providers and os packagers to split sub features of one package and distribute the chunks separately. It will virtually merge dirs with the same name under some circonstances, and won't be importable in some others. You also lose the benefit of having an init file in which you explicitly setup the object you expose for import.
- scrollaway 7y agoAmen. Much as I wish python importing was as powerful as js, that ship has sailed and we're nowhere close to getting rid of the dunder-init file. Also pathlib is .. urgh. This is one of those that shouldn't be in the stdlib. I don't get why it is, but requests isn't.
- sametmax 7y agoJs has no import system. Bundlers do, and they are terrible at it though.
- ChrisSD 7y agoModern js has native modules: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- sametmax 7y agoWhich don't work in the browser, the main place we use JS.
- ChrisSD 7y agoIt does work in all modern browsers except Edge. Which I grant you might be a sticking point. However, Edge is moving to Chromium in the near future.
- sametmax 7y agoFirefox 66: import('/modules/mon-module.js') SyntaxError: dynamic module import is not implemented So no network access, and of course no FS access. Hence, doesn't work in the browser. You need a lib or a bundler.
- ChrisSD 7y agoI'm also using Firefox 66 and it works for me. Here's a live example: https://mdn.github.io/js-examples/modules/basic-modules/ https://mdn.github.io/js-examples/modules/basic-modules/ Source code and other examples are at: https://github.com/mdn/js-examples/tree/master/modules https://github.com/mdn/js-examples/tree/master/modules
- masklinn 7y ago> Much as I wish python importing was as powerful as js In what way is python importing less powerful than JS? My experience is very much the opposite (to the extent that JS even has an import system). > I don't get why it is, but requests isn't. Because pathlib was proposed (PEP 428), considered useful and self-contained. The requests project does not want it to be included (this was discussed at length back in 2015), and it would require merging chardet and urllib3 into the stdlib first.
- scrollaway 7y agoMy post was unclear, it's fair that base JS doesn't have an import system; I was talking about ES6 imports. And yes I'm aware why requests isn't in the stdlib, but that doesn't make it "correct". And chardet should definitely be in the stdlib. I mean, ffs, `mimetypes` is in the stdlib. urllib3 is more contentious but honestly? It probably should be as well.
- aldanor 7y agoWhat's your deal with pathlib?
- donarb 7y agoFor starters, I'd say that overloading the multiplication operator to concatenate strings and insert path separators is one very bad code smell.
- nerdponx 7y agoWe already have SQLite, difflib, et al. What's so bad about pathlib? I use it all the time, it's great.
- anentropic 7y agopathlib is nice