instance as module

Robin Becker robin at reportlab.com
Fri Jun 19 05:29:14 EDT 2015


I'm trying to overcome a recursive import issue in reportlab.

Module reportlab.rl_config uses various sources (eg ~/.reportlab_settings) to 
initialize various defaults eg canvas_basefontname. If a user wants to utilize 
reportlab to set up such a default, it's not possible to do so in the settings 
file because a recursive import would occur. Normal defaults are always 
primitive python objects eg float/int/str etc etc.

If I make the rl_config module into an object (using the GvR blessed approach 
indicated here http://stackoverflow.com/questions/2447353/getattr-on-a-module) 
then I can use callables in the settings module and get them evaluated lazily 
which overcomes the recursion.

Are there any gotcha's that need to be considered when using this instance as 
module approach? My trial implementation seems to work, but it's not clear 
exactly how a module differs from an instance. I have set various dunders on the 
instance eg __file__, __doc__, __all__ & __name__ and I made the object a borg, 
but it still seems a bit hacky.
-- 
Robin Becker




More information about the Python-list mailing list