Design question.

Lacrima Lacrima.Maxim at gmail.com
Mon Jul 20 09:42:23 EDT 2009


On Jul 20, 4:05 pm, Jean-Michel Pichavant <jeanmic... at sequans.com>
wrote:
> Lacrima wrote:
> > Hello!
>
> > I am newbie in python and I have really simple question, but I need
> > your advice to know how to do best.
> > I need to store a number of dictionaries in certain place. I've
> > decided to store them in a separate module.
> > Like this:
> > dicts.py
> > -----------------------
> > dict1 = {....}
> > dict2 = {....}
> > dict3 = {....}
>
> > Then, when I need any dictionary, I can access it:
> > import dicts
> > dicts.dict1
>
> > Is it a good practice? Or should I store them as class attributes or
> > anything else?
>
> > Thanks in advance.
>
> > With regards, Max
> > (sorry if my English isn't very proper)
>
> Defining dict as a module attribute ic correct, but try to answer the
> following question:
>
> Is dict1 an attribute/property/declension of the object/entity defined
> by the module dicts ?
> If yes, then your design is correct.
>
> An correct example:
> fruits.py
> ------------
> apple = {}
> banana = {}
>
> An incorrect one:
> fruit.py
> -----------
> apple={}
> bicycle={}
>
> Basically, the rule is very straightforward, set your dict as a module
> attribute only if its one of its attribute (very nice paraphrase !)
> Most of people (including me) tend to have a  module, where they put
> everything they have no idea about their location. This is a bad habit
> and result from a uncontrolled/undocumented design. Usually documenting
> objects in such modules is really painful.
>
> Your proposal is fine from a python syntax point of view. I can't tell
> of your design with names like (dicts.py and dict1,dict2) hoping you do
> not intend to name them that way.
>
> JM

Hi, Jean-Michel!

Thanks for your answer.
I am not going to have names like dicts.py and dict1,dict2. That was
just example. I wanted to know if it is correct to have dictionaries
on a module level. Now I know that this is correct. I want to collect
in one module a number of dictionaries, every of which describes a
separate web service. And my function in another module will import
required dictionary, depending on what web service should be used.
Thanks again for the help.

With regards,
Max.
(sorry if my English isn't very proper)



More information about the Python-list mailing list