File / Module Structure in Python

So, I'm just starting out with Python and am currently working my way through http://diveintopython3.ep.io/ . The code examples are good, but the vast majority of them are small four-line snippets, and I want to see a little more general picture.

As I understand it - and correct me if I am mistaken - every .py file becomes a "module", and a group of modules in a directory becomes a "package" (at least it does if I create a file __init__.pyin this directory). What is it if I do not have a file __init__.py?

So what does each “module” look like? Usually I define only one class in a file? Does anything else in this file do besides the class definition and possibly several commands import?

+3
source share
3 answers

What is it if I do not have a __init__.pyfile?

This is just a folder.

Do I generally define only one class in a file?

It depends. Not necessary.

Does anything else in this file do besides the class definition and possibly several import commands?

You can put whatever you want. All that is valid python at least.

+5
source

, , , __init__.py , sqlite3 SimpleHTTPServer

+1

Falmarri answers this pretty well, but just adds:

__init__.pymay be an empty file (and usually this), but it can also execute initialization code for the package or install __all__.

0
source

Source: https://habr.com/ru/post/1785261/


All Articles