This page tries to summarize the results of the discussion on sage-devel I formulate everything here as strict rules, but of course every guideline can have a valid use case for not following it, but stating things in a political correct way would just obfuscate the main ideas.

Possible Guide Lines

Execute as less as possible code in modules which are imported at startup


Opinion on sage-dev

Most people find it a good idea.

Solutions to concrete problems

If you want to have some precomputed data available in the top level of your module, don't precompute at startup time, use the cached_function decorator instead.

Examples

special_matrix_list = initialize_special_matrix_list()

@cached_function
def special_matrix_list():
    return initialize_special_matrix_list()

Use relative imports


Opinion on sage-dev

Mixed, most think it is not a good idea.

Pros

Cons

Start making use of __init__.py files

Opinion on sage-dev

N/A since this idea has to be developed more first

Pros

Cons

Don't import specific functions or classes from another module


opinion on sage-dev

To early to tell.

Pros

Cons

import sage.rings.all

will force 4? lookups at runtime when later executing

sage.rings.all.ZZ(3)

while

from sage.rings.all import ZZ

will cause 1? lookup at runtime when later executing

ZZ(3)

Solutions to concrete problems

To be provided

Examples

bad: import module.a.b.c.d  #slow at runtime

good: from module.a.b.c import d

bad:

from module.sub_module import some_function #cause of problems with possible circular imports

from module.sub_module import some_class #cause of problems with possible circular imports

good:

from module.sub_module import some_sub_sub_module

import module

valid use case to ignore this guideline: To be provided

Links/Recources