[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

__init__ patterns

I will put imports into my __init__ files, so that I can import things from the module directly instead of having to import from a file in the module.

I almost never put code in the __init__'s, I have a couple of times put in something that was designed to modify which routine was imported (i.e., pull this module in if it's a windows box, or that one for other.  Or, use this routine for py 2.x and that one for 3.x.  Even there, I prefer to do it in the files themselves, but sometimes it's just easier to do it at the __init__ level.

I may also put common documentation in the __init__ file that explains the file structure for the module,  how to interact with it, etc...

For many shared functions, I try to maintain a common helper module that is shared by a number of programs, or a helper file in a module if I need to rather than putting it in the __init__


> -----Original Message-----
> From: Python-list <python-list-bounces+d.strohl=f5.com at python.org> On
> Behalf Of Tim
> Sent: Thursday, August 30, 2018 6:01 AM
> To: python-list at python.org
> Subject: __init__ patterns
> EXTERNAL MAIL: python-list-bounces+d.strohl=f5.com at python.org
> I saw a thread on reddit/python where just about everyone said they never
> put code in their __init__ files.
> Here's a stackoverflow thread saying the same thing.
> https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-
> package-init-py-files
> That's new to me. I like to put functions in there that other modules within
> the module need.
> Thought that was good practice DRY and so forth. And I never do 'from
> whatever import *'
> Ever.
> The reddit people said they put all their stuff into different modules and leave
> init empty.
> What do you do?  I like my pattern but I'm willing to learn.
> thanks,
> --Tim
> --
> https://mail.python.org/mailman/listinfo/python-list