On 2017-11-04 19:42, Stefan Ram <r...@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
| |   | h...@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to