【问题标题】:Want to add function to a module想要为模块添加功能
【发布时间】:2021-01-08 21:15:20
【问题描述】:

我想在数学模块中添加一个函数log100()

def log100(number):
  ans = math.log(number,100)
  return ans

如果不使用“os”模块来添加目录,我看不到怎么做。

【问题讨论】:

标签: python-3.x


【解决方案1】:

虽然这不是最好的主意——你可能会因为混淆标准模块而让后来阅读你的代码的人感到困惑——你可以做类似的事情

math.log100 = lambda x: math.log(x, 100)

def log100(x):
    return math.log(x, 100)

math.log100 = log100

,然后可以称为

math.log100(10) # Output 0.5

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多