Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Code in __init__.py, is it bad form?

2 views
Skip to first unread message

Michael Crute

unread,
Feb 23, 2009, 8:41:59 PM2/23/09
to pytho...@python.org
Is it bad form (i.e. non-pythonic) to have code in your __init__.py
files? I know this is subjective so I'm just looking for the general
consensus. I've heard both sides of the story and I personally feel
its okay if the code pertains to the whole module but have an open
mind about the matter. If you object to code in __init__.py why, and
what alternatives do you propose?

--
________________________________
Michael E. Crute
http://mike.crute.org

God put me on this earth to accomplish a certain number of things.
Right now I am so far behind that I will never die. --Bill Watterson

Steve Holden

unread,
Feb 23, 2009, 9:03:18 PM2/23/09
to pytho...@python.org
Michael Crute wrote:
> Is it bad form (i.e. non-pythonic) to have code in your __init__.py
> files? I know this is subjective so I'm just looking for the general
> consensus. I've heard both sides of the story and I personally feel
> its okay if the code pertains to the whole module but have an open
> mind about the matter. If you object to code in __init__.py why, and
> what alternatives do you propose?
>
No, it's absolutely fine. One common usage is to import symbols from
sub-modules so that they are available from a simple import of the package.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Michael Crute

unread,
Feb 23, 2009, 9:21:57 PM2/23/09
to pytho...@python.org
On Mon, Feb 23, 2009 at 9:03 PM, Steve Holden <st...@holdenweb.com> wrote:
> Michael Crute wrote:
>> Is it bad form (i.e. non-pythonic) to have code in your __init__.py
>> files? I know this is subjective so I'm just looking for the general
>> consensus. I've heard both sides of the story and I personally feel
>> its okay if the code pertains to the whole module but have an open
>> mind about the matter. If you object to code in __init__.py why, and
>> what alternatives do you propose?
>>
> No, it's absolutely fine. One common usage is to import symbols from
> sub-modules so that they are available from a simple import of the package.

Yeah, I use it often for that I'm talking more about stuff like
utility functions, main methods, etc...

-mike

Benjamin Peterson

unread,
Feb 23, 2009, 9:41:33 PM2/23/09
to pytho...@python.org
Michael Crute <mcrute <at> gmail.com> writes:

> On Mon, Feb 23, 2009 at 9:03 PM, Steve Holden <steve <at> holdenweb.com> wrote:
> > No, it's absolutely fine. One common usage is to import symbols from
> > sub-modules so that they are available from a simple import of the package.
>
> Yeah, I use it often for that I'm talking more about stuff like
> utility functions, main methods, etc...

There's no overarching Python doctrine forbidding you to do it. It has simply
become a matter of taste. Personally, I tend to put utility functions in their
own module (eg. util), but I do use __init__ for main methods and such.


Ben Finney

unread,
Feb 23, 2009, 9:43:06 PM2/23/09
to
Michael Crute <mcr...@gmail.com> writes:

> Is it bad form (i.e. non-pythonic) to have code in your __init__.py
> files?

No, it's good form, but *only* for code that truly pertains to the
entire package. Anything that can reasonably be in a module other than
‘__init__’, should be.

That leaves the following things suitable for code in ‘__init__’,
OTTOMH:

* Metadata attributes for the package (e.g. version string).

* Docstring for the package

* ‘from foo import bar’ to present attributes of the package that are
actually implemented in a module, which should be just about
everything in the package.

> If you object to code in __init__.py why

I object only to code that isn't clearly “relates to the entire
package and can't reasonably be implemented in a separate module”.

If you're going to the effort of making a package (rather than just
coding the namespace as a single module), then the implementation
should be in modules other than ‘__init__’ to allow easier re-use,
re-factoring, and testing.

--
\ “When I wake up in the morning, I just can't get started until |
`\ I've had that first, piping hot pot of coffee. Oh, I've tried |
_o__) other enemas...” —Emo Philips |
Ben Finney

alex23

unread,
Feb 23, 2009, 9:43:15 PM2/23/09
to
On Feb 24, 12:21 pm, Michael Crute <mcr...@gmail.com> wrote:
> Yeah, I use it often for that I'm talking more about stuff like
> utility functions, main methods, etc...

Personally, I use it only for those tasks related to 'initialising'
the package, a la the importing of various modules for convenience
sake (as mentioned by Steve).

I don't expect a class to define its method within its __init__, so
I'd find it unusual for a package to be defining any kind of
functionality within its __init__.py. I wouldn't _not_ use your code,
but I might eye it warily... :)

Michael Crute

unread,
Feb 23, 2009, 9:48:36 PM2/23/09
to pytho...@python.org
On Mon, Feb 23, 2009 at 9:41 PM, Benjamin Peterson <benj...@python.org> wrote:
> Michael Crute <mcrute <at> gmail.com> writes:
>> On Mon, Feb 23, 2009 at 9:03 PM, Steve Holden <steve <at> holdenweb.com> wrote:
>> > No, it's absolutely fine. One common usage is to import symbols from
>> > sub-modules so that they are available from a simple import of the package.
>>
>> Yeah, I use it often for that I'm talking more about stuff like
>> utility functions, main methods, etc...
>
> There's no overarching Python doctrine forbidding you to do it. It has simply
> become a matter of taste. Personally, I tend to put utility functions in their
> own module (eg. util), but I do use __init__ for main methods and such.

Okay, so assuming I have decent judgment it sounds like I'm not doing
anything patently wrong. Thanks for the input.

Carl Banks

unread,
Feb 23, 2009, 10:16:54 PM2/23/09
to
On Feb 23, 5:41 pm, Michael Crute <mcr...@gmail.com> wrote:
> Is it bad form (i.e. non-pythonic) to have code in your __init__.py
> files? I know this is subjective so I'm just looking for the general
> consensus. I've heard both sides of the story and I personally feel
> its okay if the code pertains to the whole module but have an open
> mind about the matter. If you object to code in __init__.py why, and
> what alternatives do you propose?


Well, I don't like when __init__ "helpfully" imports stuff I don't
need. However, most single modules have lots of code I don't need,
too, and I never concern myself with them, so it's probably a
misplaced irk.

Go ahead and use it.


Carl Banks

0 new messages