[TSBOAPOOOWTDI]using names from modules
Cameron Simpson
cs at cskk.id.au
Sat Nov 4 17:34:47 EDT 2017
On 04Nov2017 20:59, Peter J. Holzer <hjp-usenet3 at hjp.at> wrote:
>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(...), ...)), [...]
I think the same. If the function is well known and idstinctively named, just
use the short form ("cos(value)").
I also use the first style, but not often.
Regarding the less clear function names, particularly things like
"os.path.join", my os.path imports often look like this these days:
from os.path import dirname, exists as pathexists, isdir as pathisdir, join as joinpath
This lets me use distinct but short names in the code. To take Peter's example:
joinpath(dirname(...), ...)
You can see I've given a distinctive name to "join", which would otherwise be
pretty vague.
Cheers,
Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)"
More information about the Python-list
mailing list