Pages

lundi 24 mai 2010

some notes about python functions and class

I had some python codes and had put them together in one file as a module. It includes a lot of functions with each function a list of input variables. The whole code is not very clean with a long list of variables in the functions.

I decide to rewrite the code. What I did is to define a new class, and the class objects are defined ( in 'def __init__') the same as the commonly used input variables.

Then in each function of the class, I use an instance of the class as the input variable. This really makes the code cleaner !

Attention1:
For a defined class, it can include functions. But the function must be either a method which defines class object values, or a method which returns some calculation results using the class objects. Iis not right to return a new varable which is not defined in the class initialization.

If a function need to return new variable, we need define the function outside the class.

Aucun commentaire:

Enregistrer un commentaire