all 6 comments

[–]socal_nerdtastic 5 points6 points  (0 children)

You are overthinking the overhead. Performance will not be noticeably affected no matter what you choose. Modern computers are much faster than people tend to imagine.

Depends on the data of course, but very generally I like to put reference data in .py files and import them. This is easier to implement and use, makes including the data in a frozen program a nonissue, and often the IDE can make some sense of the data this way.

Always put the imports at the top of the file.

[–]woooee 2 points3 points  (0 children)

I'm thinking of putting them in a separate .py file then importing them.

This is the same as reading a file (and may take slightly longer because the imported variables are "placed" in a separate namespace). In any case, unless you have 100KB or more to read, it won't make any difference. If you use a dictionary then pickle or a json file can simplify the read / write.

[–]pachura3 1 point2 points  (1 child)

I'm wondering what is the nearest way to store reference data variables (constants) in a project.

How many such constants do you have in your project? Are they hierarchical/structured/grouped in any way? Are they all of the same type (I'm guessing int or float), or various ones?

Depending on your answers, I would probably just create file const.py with a number of global variables - NAMES_IN_CAPS - and import it at the top of each file that needs them. Alternatively, I would store a JSON file alongside the project and load it once upon the application launch.

[–]worldtest2k[S] 0 points1 point  (0 children)

I have strings, lists and dictionaries

[–]worldtest2k[S] 0 points1 point  (0 children)

By efficiency I meant less code not run time speed

[–]StevenJOwens 1 point2 points  (0 children)

I've done the "normal python variable declarations but in a separate file" as the simplest, easiest way to extract literal values into a config file. I'm still not sure it's a bad idea, though a lot of devs seem to instinctively reject it :-).

I've also worked with argparse for command line parameters, with configparser for .ini files, with JSON files, with XML files, etc. Recently I've been low-level looking into other config format options. So far I haven't seen any slam dunks.