简体   繁体   English

Python:如何从元组切片而不是元组传递元组元素

[英]Python: how to pass tuple elements from a tuple slice rather than tuple

I'm learning Python and haven't yet learned completely about how arg arrays are handled in function calls.我正在学习 Python 并且还没有完全了解如何在 function 调用中处理 arg arrays。 That's what my question is about: I'm passing a slice from a tuple into a function expecting the function will see multiple arguments, but it's just seeing one.这就是我的问题所在:我将一个切片从一个元组传递到一个 function 期望 function 会看到多个 arguments,但它只是看到一个。 It's not clear to me if this is a normal pattern that I need to work with in the called function, or if I should be doing something differently.我不清楚这是否是我需要在名为 function 中使用的正常模式,或者我是否应该做一些不同的事情。

In my project, I'm parsing a binary file format (OpenType font file).在我的项目中,我正在解析二进制文件格式(OpenType 字体文件)。 The format is a complex tree of many different struct types: some structs are comprised of basic binary types supported by struct.unpack , but some have members that are themselves complex structs.该格式是许多不同结构类型的复杂树:一些结构由struct.unpack支持的基本二进制类型组成,但有些具有本身就是复杂结构的成员。 So, I'm creating a pattern for handling these in which struct.unpack is called with a format string for the entire complex struct, and then slices of the returned values are passed to the member struct types to interpret.因此,我正在创建一种模式来处理这些问题,其中使用整个复杂结构的格式字符串调用struct.unpack ,然后将返回值的切片传递给成员结构类型以进行解释。

For example, for a given table type—let's call it Foo , the combined format string would be '>HHH2H' .例如,对于给定的表类型——我们称之为Foo ,组合的格式字符串将是'>HHH2H' The method in Foo that handles parsing for that type knows that this is divided into two members, a uint16 followed by a struct. Foo中处理该类型解析的方法知道它分为两个成员,一个 uint16 后跟一个结构。 (So, the format string was a combination of ">H" and ">HH2H" .) The call to struct.unpack returned a tuple of 5 elements. (因此,格式字符串是">H"">HH2H"的组合。)对struct.unpack的调用返回了一个包含 5 个元素的元组。 A slice of four of those elements will get passed to a method in the member type for it to interpret.其中四个元素的切片将被传递给成员类型中的方法以供其解释。

Here's this bit of code for one particular case:这是一个特定情况的这段代码:

    vals = struct.unpack(
        PaintFormat1._packedFormat,
        fileBytes[:PaintFormat1._packedSize]
        )

    color = ColorIndex.interpretUnpackedValues(vals[1:])

PaintFormat1._packedFormat is the format string for the entire complex structure, '>HHH2H' . PaintFormat1._packedFormat是整个复杂结构'>HHH2H'的格式字符串。 fileBytes is a byte sequence that starts at the start of the PaintFormat1 structure. fileBytes是从PaintFormat1结构的开头开始的字节序列。

Now, I've defined the ColorIndex.interpretUnpackedValues method with a signature as follows:现在,我使用如下签名定义了ColorIndex.interpretUnpackedValues方法:

    def interpretUnpackedValues(*vals):

        assert len(vals) == ColorIndex._numUnpackedValues

ColorIndex._numUnpackedValues is the expected number of arguments. ColorIndex._numUnpackedValues是 arguments 的预期数量。 The problem is that vals[1:] (from the calling context) is getting passed as a tuple, not as elements of the tuple.问题是vals[1:] (来自调用上下文)作为元组传递,而不是作为元组的元素。 So, for instance, in one call the arguments received are ((17, 0, 0, 0),) , not (17, 0, 0, 0) .因此,例如,在一次调用中,收到的 arguments 是((17, 0, 0, 0),) ,而不是(17, 0, 0, 0) Thus, len(vals) is 1, and the assert is tripped.因此, len(vals)为 1,并且断言被触发。

Another way to put this: I was expecting to write the assert using len(vals) , not len(vals[0]) .另一种说法是:我希望使用len(vals)而不是len(vals[0])来编写断言。 And then to access the individual values as vals[0] , vals[1] , etc., not vals[0][0] , vals[0][1] ...然后访问单个值作为vals[0]vals[1]等,而不是vals[0][0]vals[0][1] ...

How do I call so that multiple arguments are passed rather than a single tuple?如何调用以便传递多个 arguments 而不是单个元组?

Or should I re-write the function definition to expect a single tuple and access member elements within that function?或者我应该重写 function 定义以期望单个元组并访问该 function 中的成员元素?

(This will be a recurring pattern I use in many places, so I want to understand and keep the code as simple and readable as possible.) (这将是我在很多地方使用的重复模式,所以我想理解并保持代码尽可能简单和可读。)

You can use * operator to unpack your arguments:您可以使用*运算符来解压缩您的 arguments:

ColorIndex.interpretUnpackedValues(*vals[1:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM