PEP: possibility of inline using of a symbol instead of "import"

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jan 7 02:27:28 EST 2011


On Thu, 06 Jan 2011 21:05:10 -0800, Chris Rebert wrote:

> On Thu, Jan 6, 2011 at 8:00 PM, Erwin Mueller
> <erwin.mueller at deventm.org> wrote:
>> On Thursday 06 January 2011 21:23:57 Robert Kern wrote:
>> > On 1/6/11 12:43 PM, Erwin Mueller wrote:
>> > > On Thursday 06 January 2011 16:28:49 dmitrey wrote:
>> > >> hi all,
>> > >> I have th PEP (I'm not sure something like that hadn't been
>> > >> proposed although):
>> > >> very often in a Python file header the following lines are
>> > >> present, like:
>> > >> from MyModule1 import myFunc1
>> > >> import MyModule2 as mm2
>> > >> from MyModule3 import myFunc3 as mf3
>> > >> etc
>> > >>
>> > >> and after several pages of code they are using somewhere, maybe
>> > >> only one time, e.g.
>> > >> r1 = myFunc1(...)
>> > >> r2 = mm2.myFunc2(...)
>> > >> r3 = mf3(...)
>> > >> It makes programs less clear, you have to scroll several pages of
>> > >> code in IDE to understand what it refers to.
>> > >>
>> > >> Regards, D.
>> > >
>> > > Why you have several pages of code in the first place? Don't you
>> > > know that you can split your code in files? Just a suggestion.
>> >
>> > Modules *should* have several pages of code. *Functions* should be
>> > limited to about a page of code at maximum.
>>
>>        I'm not quite familar with Python development, but why
>>        should modules
>> be so big that the user is lost in the code?
> 
> They shouldn't, but due to Python's conciseness, the amount of code a
> module can hold before it becomes unwieldy is a good bit greater than
> some more verbose languages. Unlike say, Java, it is quite normal to
> have several classes, functions, and constants in a single Python
> module. "Several pages" is not an unreasonable upper bound for a Python
> module.

Have a look at the decimal.py module in the standard library. That's 
nearly 5800 lines, including blanks, comments and docstrings, for 18 
classes and 19 top-level functions, over 88 pages. I'd call that the 
maximum size I'm comfortable with a single module. "Several pages" is 
nothing to fear.

I recently split a module I'm working on into a package of seven modules 
(plus about the same again for tests). My module was about 2500 lines, 
for about 50 classes and functions, and I expect it to grow by another 
dozen or so functions over the coming months.

Splitting a single module into multiples doesn't happen for free. You 
have to worry about imports and dependencies that simply aren't an issue 
in a single module. But on the plus side, I was able to rename a bunch of 
similar-but-different functions from:

module.function
module.function1

to a much more sensible:

module.function
module.submodule.function



-- 
Steven



More information about the Python-list mailing list