python中max函数有重复数字时_当您有一个名为max的变量时,使用Python的max函数?...

Python includes the built in max() function. However, despite it being built in it is not a keyword. That is to say, you are allowed to do max=4. This makes sense since the maximum of something comes up a lot. But! If you use max as a variable, then it disables use of the max function in that scope.

So if you do:

max = 4

max(1, 2)

You will get an error of int object not callable. Again, makes sense. But is there any way to specify that you would like the max function? Like a std.max()? This goes for all other built in functions as well.

解决方案

The __builtin__ (Python 2) / builtins (Python 3) module provides another way to access all built-in/standard identifiers for cases like this:

>>> import __builtin__

>>>

>>> __builtin__.max is max

True

>>>

>>> max = 2

>>> __builtin__.max([0, max])

2

import __builtin__ as builtins

def random_integer(min, max):

random_integer.seed = builtins.max(10101, ( # looks random enough, right?

((random_integer.seed * 3 - 210) % 9898989) >> 1) ^ 173510713571)

return min + (random_integer.seed % (max - min + 1))

random_integer.seed = 123456789

This module is not normally accessed explicitly by most applications, but can be useful in modules that provide objects with the same name as a built-in value, but in which the built-in of that name is also needed.

In order to get rid of the confusion between __builtin__ and __builtins__, it was decided to rename __builtin__ (the module) to builtins, and to leave __builtins__ (the sandbox hook) alone.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值