Frage

I have been a PHP developer for the last decade, but figured it might be a wise move to learn another language. Like Python.
Python in and off itself isn't so hard to understand when you've got a background in programming.

What I can't yet understand though is how to structure my application and how to layout my classes. In PHP the convention is to do something like this: Vendor\Module\SubModule\Class, which has a direct mapping to a directory and file on the file system, Class.php being the file where the Class is defined.

The closest thing I have gotten in Python is this: Module.Class, where Module.py is the file where Class is defined. So with this structure, all classes belonging to this module should be defined in Module.py.

I don't want to emulate the PHP way of doing things, I just provided it for context.

So the question really is: What is the pythonic way of structuring an application?

Edit
Alright, maybe the question is too broad. To learn python I am converting some php scripts to Python. I have a script that imports files with cluster information into a database. My php structure looks like this:

.
`-- Company
    |-- Cluster
    |   |-- Cluster.php
    |   |-- Import
    |   |   |-- Array.php
    |   |   `-- Csv.php
    |   |-- Import.php
    |   |-- Map.php
    |   `-- Sync.php

So I have an Import interface Company\Cluster\Import.php which is implemented by two scripts: Company\Cluster\Import\Array.php and \Csv.php
Further I have some classes that take care of handling and preparing the incoming data to send off to the DAO.

How might I structure this application in Python?

War es hilfreich?

Lösung

It is a very open question, but I would say your problem is that you are missing the concept of package.

A package is a folder that contains modules, and other packages, allowing for nesting as much as you want. For Python to identify a folder as a package, it has to contain an __init__.py file (for the moment, just an empty file with that name is enough).

In python a module is a .py file. I would recommend you not to use that name for folders (aka packages).

Like this, you can create arbitrary structures. For instance, taking your example:

main.py
vendor/
    __init__.py
    mymodule/
        __init__.py
        mysubmodule/
             __init__.py
             any_class.py

Then you define your AnyClass in any_class.py. After that you can import from main.py this way:

from vendor.mymodule.mysubmodule.any_class import AnyClass

Bear in mind that you dont have to import explicitly the class. You can import the module (== python file), the package or whatever your want. Following examples are perfectly valid, although less common.

import vendor
my_obj = vendor.mymodule.mysubmodule.any_class.AnyClass()

from vendor import mymodule
my_obj = mymodule.mysubmodule.any_class.AnyClass()

...

from vendor.mymodule.mysubmodule import any_class
my_obj = any_class.AnyClass()

And another thing to keep in mind is, in python you are not forced to have one class per file like in java. You are free to have more, and if they are tightly related, very often makes sense to have them all in the same module.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top