当您有一个名为max的变量时,使用Python的max函数?

编程入门 行业动态 更新时间:2024-10-26 11:15:36
本文介绍了当您有一个名为max的变量时,使用Python的max函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Python包含内置的max()函数。但是,尽管内置了它,但它不是关键字。也就是说,允许您执行 max = 4 。这是有道理的,因为最大程度地增加了一些东西。但!如果将max用作变量,则它将禁用该范围内的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.

因此,如果您这样做:

max = 4 max(1, 2)

您将得到 int对象的错误不可通话。再次,有道理。但是有什么方法可以指定您想要max函数吗?就像 std.max()一样?这同样适用于所有其他内置函数。

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.

推荐答案

__ builtin __ (Python 2) / builtins (Python 3)模块提供了另一种方法来访问所有内置/标准标识符,例如:

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.

Python 3中的名称更改是 PEP 3100中描述的核心语言更改:

The name change in Python 3 is part of the "core languages" changes described in PEP 3100:

消除 __ builtin __ 和 __ builtins __ ,因此决定重命名 __ builtin __ (该模块)放置到 builtins 中,而只剩下 __ builtins __ (沙盒挂钩)。

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.

更多推荐

当您有一个名为max的变量时,使用Python的max函数?

本文发布于:2023-11-23 05:11:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当您   变量   有一个   函数   max

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!