[Tutor] creation of a module

Kent Johnson kent37 at tds.net
Fri May 13 12:10:02 CEST 2005


Cedric BRINER wrote:
> hi,
> 
> 1)
> I'm trying to create my _first_ own module. I've decided to write each class into a separate file.
> 
> /MyModule|-bunch.py
>          |-a.py
>          |-b.py
>          `-c.py

You also need MyModule/__init__.py to signal to Python that MymModule is a package. (Actually you 
are creating a package containing several modules.)

> 
> {a,b,c}.py contains respecetively classA,classB,classC more some unittest
> and bunch.py will contains some usefull function using the class{A,B,C}
> 
> I'd like that when I import MyModule (`import MyModule')OB
>  I'll see from it:
> MyModule.classA
>         .classB
>         .classC
> 	.<function1 in numch.py>
>         .<function2 in numch.py>
>         ...
> without seeing MyModule.{a,b,c}

In MyModule/__init__.py put
from MyModule.a import classA
from MyModule.b import classB
from MyModule.c import classC

This creates package-level attributes for the classes.

> 2) does someone now how to do this:
> x=3
> with a function like:
> assign('x',3)

Can you say why you want to do this? It is possible but generally it is better to use a dict:
values = {}
values['x'] = 3

Kent



More information about the Tutor mailing list