gpt4 book ai didi

python - 导入模块然后导入特定函数 - 更Pythonic的方式来处理导入

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:59 24 4
gpt4 key购买 nike

我正在考虑哪个更Pythonic,

import math
from math import sqrt

import math
sqrt = math.sqrt

我编写过多次使用 sqrt 的代码,因此每次都使用 math.sqrt 有点奇怪。在此代码中,有时我会使用 math.(something) 例如 math.pimath.ceil 等。现在您可以使用前一种或后一种方式来使用 sqrtmath。有什么理由可以确定哪个更好?

最佳答案

根据您的评论,前一个

import math
from math import sqrt

更具可读性(并且Pythonic。)尽管最好的解决方案是简单地使用import math,然后使用math.sqrt,但此解决方案是可以接受的。

关于python - 导入模块然后导入特定函数 - 更Pythonic的方式来处理导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53187117/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com