>> If we were inspecting this code, we wouldn't know whether or not
>> get_cat() comes from CatApp A or B, which makes troubleshooting more
>> difficult, especially if both apps (or modules) define a get_cat()
>> function. It would be much better to say:
>>
>> from CatAppA import get_cat
>> from CatAppB import say_meow
>>
>> my_cat = get_cat('Katy Purry')
>>
>> Now we know that get_cat() comes from CatAppA.
>
> Yes, that's a great description of the reason to avoid `import *`.
>

Hey, thanks. :-D


>> The other aspect to consider is memory use. If a module defines 500
>> functions, doing 'import modulename' will load all 500 in memory, rather
>> than just picking out the one or two you might use.
>
> This isn't true. When you first import a module (no matter what syntax
> you use), its full code is executed, resulting in a module object with
> all of its top-level objects and attributes defined, which is placed
> into the `sys.modules` dictionary. So every function or class defined in
> the module will be in memory no matter what.
>
> The import syntax you use determines only which functions/objects you
> get references to in the current module, it doesn't change memory usage
> at all.
>
> (Not to mention that, apart from extreme cases, memory usage of your
> code objects themselves will be negligible compared to your data.)
>

I'll admit when I'm wrong. This is one of those instances that proves
you can't believe everything you read on the Internet. I had read
something to this effect some years ago, but haven't invested a ton of
research energy.

I'll defer to Carl, who likely has immeasurably more experience in
this area than I do based on responses I've seen from him in other
threads.

>> Aside from that, all of your references would be prefixed with
>> 'modulename', which may or may not make the code more readable.
>
> This isn't related to `import *`, it depends on the distinction between
>
>    from somemodule import some_func
>
> vs
>
>    import somemodule
>
> In the latter case, you would use `somemodule.some_func` in your code;
> in the former you'd use `some_func`. Both of those are equally explicit;
> the choice between them is really a matter of preference, conciseness,
> and avoiding naming clashes. If I am using only one or two things from a
> module (especially if I am using them many times), I'll usually use
> `import from` to make the references shorter. If I am using many things
> from the module, I'll prefer the latter form (and sometimes shorten the
> module name with an alias, via `import somemodule as sm`).
>

This is the exact point that I was driving at, however in re-reading
what I had wrote, I can see that I wasn't clear. Thanks for the
clarification.


Thanks Carl, I always enjoy reading your responses (even the ones
where you are disproving me ;-D). Your insights are always on point
and well stated.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUY0yscq7vPgH8Qc4%2BNSnLLxcqnmD2UyBRBXAvDggJoGg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to