Proposal: Inline Import

?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Shane said:
Do you have any ideas on how to improve the process of maintaining
imports? Benji's suggestion of jumping around doesn't work for moving
code and it interrupts my train of thought. Sprinkling the code with
import statements causes a speed penalty and a lot of clutter.

You didn't detail your implementation strategy for that feature very
much, but the most natural way of implementing it would also cause the
same speed penalty as sprinkled import statements.

Regards,
Martin
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Shane said:
Thoughts?

I have two reasons to dislike it:
1. It's a language change. Others have pointed out that you can achieve
the same without a language change; it would be easy to write

name_expr = _import.re.compile('[a-zA-Z]+')

2. In the form in which you have written it, I believe it is
unimplementable (or, else, an implementation would have
counter-intuitive border cases). For example, what would

.xml.sax.expatreader.ExpatParser.__init__(self)

mean? From the description, it appears that it would be

from xml.sax.expatreader.ExpatParser import __init__ as \
__hidden_xml_sax_expatreader_ExpatParser___init__

which, of course, wouldn't work, because ExpatParser is
not a module.
3. As in any good list of two items, I have a third complaint:
for packages, this would be tedious to type (as the sax
example illustrates)

Regards,
Martin
 
B

bruno at modulix

Mike said:
(snip)


Has it ever occured to you that if you're cutting and pasting 500 line
blocks, you're doing something fundamentally wrong? One of the points
of modules and OO is that you don't *have* to do things like
that. Cut-n-paste means you wind up with two copies of the code to
maintain,

Mike, has it ever occured to you that this could be refactoring, not
copy_paste ?-)

(for the record, I too frequently *move* - not 'copy_paste' - big chunks
of code, at least at the beginning of a project, or before a major
evolution)
 
A

adam

When I'm feeling too lazy to do imports while moving large blocks of
code, I use this little hack. It lets me proceed with checking whether
the move does what I wanted and at the end I fix the imports and remove
the try/except wrapper. I think it would achieve your desired result
and not have an impact on the language itself.

try:
#your main code here
print string.upper("blah")
except NameError, error_value:
mod_name = error_value.args[0][error_value.args[0].find("'") +
1:error_value.args[0].rfind("'")]
try:
__import__(mod_name)
print "imported %s" % mod_name
except:
print "NameError: %s" % error_value.args[0]
pass

-adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top