Design question.
Marcus Wanner
marcusw at cox.net
Mon Jul 20 13:42:55 EDT 2009
On 7/20/2009 9:42 AM, Lacrima wrote:
> 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)
Yeah, you can put just about anything in a separate module/file, but
putting unrelated things in the same module is bad...
For example, if you have apple and banana in a module, and had a
function called slicefruit() to do something to those variables in there
with them, then that would be good. But if you put bicycle and car and
adjustbrakes() in the module with the fruits, or mixed the two groups in
several modules, that would be bad.
Marcus
More information about the Python-list
mailing list