内置函数以应用具有多个变量的单个表达式

2024-04-20 07:46:45 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有一个现有的Python内置函数,它分配两个或多个变量,并在分配单个表达式时返回多个值?例如:

假设:

attrs, patterns, weight = [] * 3

同:

attrs, patterns, weight = [], [], []

Tags: 函数表达式内置attrspatternsweight
3条回答

如果你想拥有你想要的行为,你可以创建一个自定义的对象,它的行为类似于下面的表达式:var1, var2, var3 = __OBJECT__ * 3

举个例子:

from collections.abc import Iterable

class Custom:   
     def __init__(self, initial=[]):
         if not isinstance(initial, Iterable):
             raise ValueError()
         self.initial = initial
         self.args = initial

     def __mul__(self, args):
         self.args = [self.initial[:] for _ in range(args)]
         return self.args

     def __iter__(self):
         for _ in range(len(self.args)):
             yield self.initial

     def __repr__(self):
         return '{}'.format(list(self))

演示:

$> a, b, c = Custom() * 3
$> print(a, b, c) # [] [] []
$> id(a) == id(b) == id(c) # False

$> a, b, c = Custom(()) * 3
$> print(a, b, c) # () () ()
$> id(a) == id(b) == id(c) # True

目前最好的语法是

attrs, patterns, weight = [[] for i in range(3)]

[], [], []相比,这并不是一个真正的改进。不过,它对复杂表达式有优势。你知道吗

如果你想要一个函数,你会遇到这样的问题:Python函数接受对象,而不是表达式。函数不能重复计算其参数表达式。如果要编写函数,必须将表达式包装成lambda或其他形式:

def n_times(constructor, n):
    return [constructor() for i in range(n)]

attrs, patterns, weight = n_times(lambda: [], 3)
# or
attrs, patterns, weight = n_times(list, 3)

有几个诱人但错误的选择:

# Mistake
attrs = patterns = weight = []
# Also a mistake
attrs, patterns, weight = [[]]*3

它将相同的列表赋给每个变量,而不是制作单独的列表,因为Python赋值不复制,列表乘法也不复制元素,只复制对元素的引用。你知道吗

a = b = c = []

所有变量都独立地设置为相同的值。(注意:相同的可变对象,但Python中的任何多重赋值都是如此。)

请参阅assignment statement的文档-允许多个target_list "="节。你知道吗

An assignment statement evaluates the expression list [...] and assigns the single resulting object to each of the target lists, from left to right.

这对我来说是一个惊喜,当我第一次尝试在立即会议!但它的工作原理和你想象的一样。你知道吗

相关问题 更多 >