How to import only one module in a package when the package __init__.py has already imports the modules?

Peng Yu pengyu.ut at gmail.com
Sun Nov 1 09:44:19 EST 2009


On Sat, Oct 31, 2009 at 11:40 PM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
> On Sat, 31 Oct 2009 22:48:10 -0500, Peng Yu wrote:
>
>>> Variables in a function are already private.  How can the names in one
>>> function be affected by other functions in the same module?
>>
>> You misunderstood me.
>>
>> If there are multiple functions or classes in a file, when I change
>> variables in a function/class, I have to make sure that they are not in
>> other functions or classes.
>
> No you don't.
>
> def f(x):
>    return x+1
>
> def g(x):
>    return x-1
>
>
> If I choose to refactor f() and change "x" to "y", why do I care about
> the internal variable inside g()?

What I mean was, for example, I only want to change 'x' in f() to 'y',
but not 'x' in g() to 'y', because the meaning of 'x' in f() and 'x'
in g() may represent different things. If both f() and g() are in the
same file, I have to make sure that I only replace 'x' in f() but not
in g(). However, if I only have f() in a file, I can simply replace
all x in the file without worrying replacing 'x' in g(). Is this
clear?

> Oh wait, I get it... you want to do a global search-and-replace over the
> entire file. *face-palm*

Yes. You get it.

> If your functions are less than one-screen full, why do you need a global
> replacement? Global replacement risks changing words in docstrings,
> comments etc that it shouldn't.

My variables in general are not as short as a single letter, like 'x',
and is descriptive. In general, they will not appear in docstrings,
unless the ones in docstrings mean the same thing, in which case it is
OK to replace the variable and the one in docstrings.



More information about the Python-list mailing list