Much like Java (or php), I'm use to seperating the classes to files.
Is it the same deal in Python? plus, how should I name the file?
Lowercase like classname.py or the same like ClassName.py?
Do I need to do something special if I want to create an object from this class or does the fact that it's in the same "project" (netbeans) makes it ok to create an object from it?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
No, you can define multiple classes (and functions, etc.) in a single file. A file is also called a module.
To use the classes/functions defined in the module/file, you will need to
import
the module/file.In Python, one file is called a module. A module can consist of multiple classes or functions.
As Python is not an OO language only, it does not make sense do have a rule that says, one file should only contain one class.
One file (module) should contain classes / functions that belong together, i.e. provide similar functionality or depend on each other.
Of course you should not exaggerate this. Readability really suffers if your module consist of too much classes or functions. Then it is probably time to regroup the functionality into different modules and create packages.
For naming conventions, you might want to read PEP 8 but in short:
and
To instantiate an object, you have to import the class in your file. E.g
or
or
You are asking essential basic stuff, so I recommend to read the tutorial.