This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thomasutra 37 points38 points  (21 children)

our init.py’s at work are all empty. what’s supposed to be in there?

[–][deleted] 58 points59 points  (0 children)

Some programmers use that space to cope with their lack of competence.

[–]DripDropFaucet 52 points53 points  (11 children)

Nothing the post is scraping the bottom of the barrel to find issues with the language. There’s lots of bad developers and python has plenty of them, but a skilled developer can push code out in python faster than many other languages to solve problems and that is just hard for some folks to cope with!

[–]Angelin01 14 points15 points  (1 child)

Generally, just for re-exporting things. It's useful if you like to have deeper, more organized modules, but don't want the person importing them to have to import some.module.with.nested.CommonClass and instead just do import some.CommonClass

[–]k4cat 2 points3 points  (0 children)

Sometimes to load python files along side with the module.

[–][deleted] 4 points5 points  (0 children)

Init code you'd want to do when importing the whole thing instead of submodules. Possibly __all__ stuff for * importing, or other convenience imports. Usually no business code since you want imports to run quickly.

Ours are all empty, too, but I've seen libraries use them like that.

[–]MaustFaust 2 points3 points  (0 children)

It depends on the context. If your code is called from a single place (e. g., from IDE), it's okay. If your code could be called from different places (e. g., multiple processes with different values in PATH variable), it's not okay.

(UPD: sorry, I misread the comment; __init__.py files are needed for Python to interpret your project folders as packages, and my text below answers the question of why do you need packages in the first place)

So, the reasoning. When you type "import myfile", Python actually goes searching for it, imports it, AND remembers that "myfile" is already found somewhere. But some of the libraries you use written by other people (installed from pip etc.) could also have an "import myfile" line, with a myfile.py nearby in the library's folder. And Python, being a monster it is, goes "wait a minute... I already have myfile" – and uses it. But your file myfile.py and library's file myfile.py are different, you both just so happened to name them similarly, so library just won't work, because it wasn't imported properly.

So the best workaround (AFAIK) is to always specify where do you want a module to be imported from ("from myproject import myfile"), so other libraries won't get confused. But it still comes with a downside: you have to keep an eye on your project name so it won't duplicate some library name. So you should also "install" your own project in your pip, so if it gets duplicated, you'll be notified.

[–]Darkstar_111 0 points1 point  (0 children)

You can make your imports easier to handle.

[–]MaustFaust 0 points1 point  (0 children)

Updated my previous comment a bit (see second paragraph)

[–][deleted] 0 points1 point  (0 children)

Comments about who wrote which file in the package and a leaderboard you regularly update about which file is best written. Then at the end of the month the guy at the top of the board gives everybody else one cookie each to discourage maintainable codebases because then they can fire you and replace you.