Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择在单个模块中对方法进行BC中断更改的方法_Python_Backwards Compatibility - Fatal编程技术网

Python 选择在单个模块中对方法进行BC中断更改的方法

Python 选择在单个模块中对方法进行BC中断更改的方法,python,backwards-compatibility,Python,Backwards Compatibility,我正在寻找一种机制,让用户选择像\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。也就是说: 它是词汇范围(仅影响模块,不影响从模块调用的任何函数) 它适用于方法(不仅仅是函数) 除了选择加入之外,它不需要我以其他方式编辑代码 出于说明目的,让我们假设我们是一个数字库,希望对类中名为div的方法进行类似于Python对from\uuuuuu\uuuu导入division的行为的更改: c

我正在寻找一种机制,让用户选择像
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。也就是说:

  • 它是词汇范围(仅影响模块,不影响从模块调用的任何函数)
  • 它适用于方法(不仅仅是函数)
  • 除了选择加入之外,它不需要我以其他方式编辑代码
出于说明目的,让我们假设我们是一个数字库,希望对类中名为
div
的方法进行类似于Python对
from\uuuuuu\uuuu导入division
的行为的更改:

class Number:
    # old behavior
    def div(self, other):
       if self.is_floating_point() or other.is_floating_point():
           return self.item() / other.item()
       else:
           return self.item() // other.item()

    # desired new behavior
    def div(self, other):
       return float(self.item()) / float(other.item())
在Python中,他们添加了一个新的未来pragma,该pragma在
\uuu div\uuu
\uuu truediv\uu
之间更改了/运算符的含义。我们不是Python,所以我们不能这么做。我们能模拟一下吗

许多让人们选择新行为的经典机制都不符合上述一个或多个标准。让我们看几个:

为函数指定不同的名称,并以旧名称导入新函数。如果div是顶级函数,这可能会起作用:

# number.py
def div(self, other):
    ... old behavior ...

def div_v2(self, other):
    ... new behavior ...

# client
from number import div_v2 as div
但是div是一种方法,它们不是传统意义上的导入方法

添加全局标志以切换行为。

DIV_NEW_BEHAVIOR = False

class Number:
    # old behavior
    def div(self, other):
       if DIV_NEW_BEHAVIOR:
           ... new behavior ...
       else:
           ... old behavior ...
class Number:
    def div(self, other):
        ... old behavior ...

class Number_v2:
    def div(self, other):
        ... new behavior ...
然而,这是一个全有或全无的标志;如果我在顶级
main.py
文件中切换它,它将影响所有代码。这与
\uuuu future\uuuu
杂注形成对比,该杂注仅适用于应用它的模块。将标志转换为上下文管理器也不起作用,因为如果您设置了上下文管理器,然后调用在另一个模块中定义的函数,即使该模块没有选择,该标志仍将被设置

制作课程的新版本。

DIV_NEW_BEHAVIOR = False

class Number:
    # old behavior
    def div(self, other):
       if DIV_NEW_BEHAVIOR:
           ... new behavior ...
       else:
           ... old behavior ...
class Number:
    def div(self, other):
        ... old behavior ...

class Number_v2:
    def div(self, other):
        ... new behavior ...
希望很明显为什么这是个坏主意?比如,如果你在Python PEP中提出要解决真除法问题,我们应该制作两种类型的整数,一种是旧除法,另一种是真除法,你会被嘲笑吗?在任何情况下,这都不符合词法范围要求,因为数字_v2将“感染”后续使用,即使它们在模块之外


\uuuuu future\uuuuuu
几乎是一种理想的机制,用于在底层库实现可以支持新旧操作模式的情况下执行可选的破坏BC的更改。但它只对Python语言实现人员可用。其他图书馆作者呢

(这是对的重新发布,并附有更多示例)