3

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

As I understand it--and correct me if I'm wrong--each '.py' file becomes a "module", and a group of modules in a directory becomes a "package" (at least, it does if I create a __init__.py file in that directory). What is it if I don't have a __init__.py file?

So what does each "module" file look like? Do I generally define only one class in the file? Does anything else go in that file besides the class definition and maybe a handful of import commands?

0

3 Answers 3

5

What is it if I don't have a __init__.py file?

It's just a folder.

Do I generally define only one class in the file?

It depends. Not necessarily.

Does anything else go in that file besides the class definition and maybe a handful of import commands?

You can put anything you want. Anything that's valid python at least.

Sign up to request clarification or add additional context in comments.

7 Comments

Why? You think it's inconceivable to have a module that's just a single class?
I have quite a few modules that don't define classes at all. I have others that define some related classes (and still don't exceed 100, 150 lines). Python is not Java - classes aren't obligatory, so you don't use them when there are better options (of course same applies for everything else).
@Falmarri : no it just because from the OP answer it seem that he is projecting java design on python so i was thinking that you can just tell , that it's not necessary in python saying it depends seem to me like if you're telling him you can program in python like Java well just sentence syntax no big deal well never mind :)
So, I could make a module bob that's just a series of functions. To use bob somewhere else, I import bob and then access those functions as bob.func_one(), bob.func_two() etc.? Or do I not need the bob. preceding the function names?
If you do a import bob, then you need to need to call each function like the way you mentioned it: bob.func_one(), etc. If you do a : from bob import func_one, this imports only func_one from bob and you call it directly: func_one() without the bob prefix.
|
1

Not really an answer, but it is always worth looking at the standard library to see how they use __init__.py in packages like sqlite3 vs. modules like SimpleHTTPServer

Comments

0

Falmarri answers it pretty well, but just to add:

__init__.py can be an empty file (and is usually is), but it can also execute initialization code for the package or set the __all__ variable.

2 Comments

Or you can put your entire code in __init__. Or put the code that you want to expose.
I've also seen (and I like it, as a library user) from pkg.subpkg import some, commonly, used, stuff so import pkg already gives you most things you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.