简体   繁体   English

不理解将元组作为参数传递

[英]Not Understanding Passing Tuple As Argument

I am really having a hard time wrapping my head around this one. 我真的很难绕过这一个。 I think I must be missing on several concepts, because it works, I am just confused how. 我想我必须缺少几个概念,因为它有效,我只是困惑如何。

I understand that I am creating a class with an initializer and two methods. 我知道我正在用一个初始化器和两个方法创建一个类。 In the initializer, I am setting two instance attributes (_queue and _index). 在初始化程序中,我设置了两个实例属性(_queue和_index)。 The leading underscore indicates they are implementation details. 前导下划线表示它们是实施细节。

I can see that the 'push' method defined takes three arguments. 我可以看到定义的'push'方法有三个参数。 The first is the object on which the method is called (aka the instance). 第一个是调用该方法的对象(也就是实例)。 The second are two variables. 第二个是两个变量。

I see that heapq.heappush() is taking two arguments: Arg1 - self._queue - basically the list created in the initializer Arg2 - A tuple 我看到heapq.heappush()有两个参数:Arg1 - self._queue - 基本上是在初始化程序Arg2中创建的列表 - 一个元组

Arg2 is what gets me. Arg2是什么让我。 How can we accept a tuple with 3 elements? 我们如何接受具有3个元素的元组? I see nothing in the heapq.heappush() documentation that says we are able to do this. 我在heapq.heappush()文档中没有看到任何内容,表明我们能够做到这一点。 It just says you put in what you want to append to, and then what you are appending. 它只是说你输入了你要附加的内容,然后是你要添加的内容。 And I don't see something at further parses the tuple to make it into the one argument that I think it should be taking. 我没有看到进一步解析元组的东西,使其成为我认为应该采取的一个论点。 When I just try passing a tuple like that into heapq.heappush() in a normal python window it fails. 当我尝试将这样的元组传递到普通python窗口中的heapq.heappush()时,它会失败。

Can someone help a novice like me understand whatever concept I seem to be missing? 有人可以帮助像我这样的新手了解我似乎缺少的任何概念吗?

Thanks so much! 非常感谢!

import  heapq
class PriorityQueue :
    def  __init__ (self):
        self._queue  =  [] 
        self._index  =  0 
    def push(self, item, priority):
        heapq.heappush(self._queue, (-priority, self._index, item))
        self._index  +=  1 #add one to the index
    def pop( self ):
        return heapq.heappop(self._queue )[- 1 ]


class Item:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return 'Item({!r})'.format(self.name)

Lets step back and think about what happens when python compiles your program. 让我们退一步思考python编译程序时会发生什么。 When python sees 当python看到

(-priority, self._index, item)

It compiles that into byte code that looks up those three pieces of data and creates a single tuple to hold them. 它将其编译为字节代码,查找这三个数据并创建一个单元tuple来保存它们。 To see, you could add a line print(type((-priority, self._index, item))) to show its a single object. 要查看,您可以添加行print(type((-priority, self._index, item)))以显示其单个对象。

Now on to the full statement 现在进行完整的陈述

heapq.heappush(self._queue, (-priority, self._index, item))

after python creates that tuple, it uses it as the second item in the method call. 在python创建该元组之后,它将它用作方法调用中的第二项。 So by the time heappush sees it, it is a single item as required. 因此,当heappush看到它时,它是一个单独的项目。 Generally, python can evaluate any expression inside the function's call and use the constructed object as the parameter. 通常,python可以计算函数调用中的任何表达式,并使用构造的对象作为参数。 Its exactly equivalent to the following code except it doesn't have to go through an intermediate variable 它完全等同于以下代码,除了它不必经过中间变量

_stuff = (-priority, self._index, item)
heapq.heappush(self._queue, _stuff)
del _stuff

It can be confusing because parens are used to make tuples and to define functions. 它可能令人困惑,因为parens用于制作元组和定义函数。 The difference is they are used in different places (that's one of the reasons you need the def keyword) so python knows what to do in each case. 区别在于它们在不同的地方使用(这是你需要def关键字的原因之一)所以python知道在每种情况下要做什么。

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

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