[TSBOAPOOOWTDI]using names from modules

Peter J. Holzer hjp-usenet3 at hjp.at
Sat Nov 4 15:59:19 EDT 2017


On 2017年11月04日 19:42, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> What is better:
>> ...
> import math
> ...
> ... math.cos ...
> ...
>> or
>> ...
> from math import cos
> ...
> ... cos ...
> ...
>> ?
>> (To me, the first is more readable, because at the site
> where »math.cos« is used, it is made clear that »cos«
> comes from math.

If I'm doing trigonometric computations I think the *second* is *much*
more readable. I'm using the well-known cosine function - that this was
imported from the math module is pure noise.
For other functions this may be less clear. I tend to use the first
style more often, although that gets a bit verbose sometimes
(os.path.join(os.path.dirname(...), ...)), And I do notice that the
second style seems to be preferred in Django.
> But I assume that the second might use
> one less name lookup and therefore is faster. What is the
> one way to do it?)

I'm not worried about performance unless there is clear performance
advantage (for real programs, not benchmarks).
 hp
-- 
 _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | | Man feilt solange an seinen Text um, bis
| | | hjp at hjp.at | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /