--
________________________________
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
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Yeah, I use it often for that I'm talking more about stuff like
utility functions, main methods, etc...
-mike
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.
> 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
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... :)
Okay, so assuming I have decent judgment it sounds like I'm not doing
anything patently wrong. Thanks for the input.
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