Python hierachy

Yoan BlancTue, 10 Oct 2006, , ,

A friend of mine, who loves C# and gonna play with Python, asked me about the file hierarchy of the pythonic module/package. For every Java or C# users it’s a bit different.

In the beautiful world of Java, a file is a class. Everything is clean and at it’s own place. The file hierarchy reflects the package hierachy. It can be the jungle in the directories.

package/Foo.java      import package
package/bar/Bar.java  import package.bar
		

With C#, which is very (very) similar to Java, the one class per file is the usual way to do, but you aren’t forced to. And the hierarchy is also the namespace hierarchy (or not). In the example bellow the both files can be in completely different namespace, strange isn’t it?

package/file.cs     using Package
package/file2.cs    using Package.Foo.Bar

With Python, it’s much more fun because a directory is a package if it contains a __init__.py file. You can use this packages with use directory. .py files in it are modules, you can use them with use directory.file (without the extention). A module (a file) can contains lots of variables, functions and classes. For every Java/C# coder it’s very messy.

package/__init__.py     use package
package/module.py       use package.module
package/foo/__init__.py use package.foo
package/foo/bar.py      use package.foo.bar

I don’t think this is messy, this is flexible. You can arrange thing exactly how you will use them.

I’ve written a very messy implementation of the State Pattern in both C# and Python just for the fun. C# is full of public, private, abstract, override... but in Python you need (if you want) to check the type of the data you receive. (I definitely love Python but, you can do a very similar job in both languages). Oh, there is no documentation because it’s very basic (google around about this design pattern if you want).

For the curious, I’ll stay in Switzerland for a while. The Dublin offices will not hire me (for the moment, keep believing).

Un pote m’a questionné à propos de la hiérarchie qui est utilisée par Python dans un projet. C’est une personne qui va sortir de l’école donc Java (triste sort) et un peu de C# par passion. Et quand on doit s’attaquer à un projet de taille respectable, venant de ces mondes là, Python peut perturber.

En Java, un fichier est une classe (quand tout va bien et qu’on respecte une certaine propreté). Ça peut juste devenir un beau bordel dès qu’un projet grossit un peu trop.

En C#, les choses sont assez semblables à Java, sauf que les choses sont un peu plus souples et qu’il est possible que la structure fichier ne colle pas à la structre du namespace.

Finalement en Python, un dossier est un paquetage et un fichier .py un module qui peut contenir moulte variables, fonctions, classes. Il est également possible de mettre tout un tas de truc dans un paquetage via le fichier __init__.py qui définit le paquetage.

En passant, le design pattern : State pattern écrit en C# et en Python. Très simple et qui peut s’avérer utile.

Pour les curieux, je reste en Suisse et vais m’installer sur Lausanne.

Digg it!, so del.icio.us, blogmarks, reddit.