使用 Deque.popleft 作为函数的参数

发布于 2024-09-10 02:10:15 字数 839 浏览 3 评论 0原文

我正在尝试存储一个命令列表,以便在 Python 中使用双端队列发送串行电缆。

我的函数“send_command”接受 3 个值;命令,一个整数。暂停和布尔等待。其定义如下。

def send_command(self, command, pause=0, wait=False):

我想做的是,而不是像这样调用这个函数:

send_command("A234", 5, True)

...或者...

send_command("B4242")

我希望能够将命令存储在双端队列列表中并使用 popleft 函数然后调用我的函数。这将允许我执行以下操作:

CommandList = deque((['A234', 5, True], ['B4242']))

...并使用 deque.append() 作为不断将内容添加到列表中的方法,然后将其发送到我的 send_command 函数。然后,我可以在需要时(或者与我的代码的串行部分一样快)放入和取出新命令。

我正在努力解决的部分实际上是使用 CommandList.popleft 或双端队列列表的任何部分作为我的 send_command 函数的参数。它看起来并不那么简单:

send_command(CommandList.popleft)

我确信这很简单,但我无法弄清楚。

有人可以帮忙吗?

非常感谢。

安迪

I am attempting to store a list of commands to send down a serial cable using deque in Python.

My function "send_command" accepts 3 values; The command, an int. pause and a boolean wait. its definition is as follows.

def send_command(self, command, pause=0, wait=False):

What I would like to do is, rather than calling this function like so:

send_command("A234", 5, True)

... or...

send_command("B4242")

I'd like to be able to store up commands inside a deque list and use the popleft function to then call my function. This would allow me to do things such as:

CommandList = deque((['A234', 5, True], ['B4242']))

...and use the deque.append() as a way to keep adding things to the list, which would then in turn be sent to my send_command function. I could then drop in and out of the list new commands when they are needed (or as quickly as the serial part of my code can do).

The part I'm struggling with is actually using the CommandList.popleft, or any part of the deque list as the args for my send_command function. It doesn't seem as straight forward as:

send_command(CommandList.popleft)

I'm sure it's something simple, but I cannot figure it out.

Can anyone help?

Thank-you kindly.

Andy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯 2024-09-17 02:10:16

可能您需要类似的东西:

obj.send_command(*CommandList.popleft())

也就是说,调用 popleft 并将结果用作 send_command 的参数列表。 self 参数表明这是一个成员函数,因此您需要在对象上调用它

另一种方法,正如我在评论中所写,是用这样的内容存储准备好的函数:

def make_command(obj, *args, **kwargs):
    def f():
        obj.send_command(*args, **kwargs)
    return f

然后您可以执行

queue.append(make_command(obj, 'ABC', whatever='else'))

以下 操作:然后执行:

command = queue.popleft()
command()

probably you need something like:

obj.send_command(*CommandList.popleft())

That is, call popleft and use the result as an argument list for send_command. self argument suggests this is a member function, so you need to call it on an object

Another way, as I wrote in the comment, is to store prepared functions with something like this:

def make_command(obj, *args, **kwargs):
    def f():
        obj.send_command(*args, **kwargs)
    return f

Then you can do

queue.append(make_command(obj, 'ABC', whatever='else'))

and then execute:

command = queue.popleft()
command()
×纯※雪 2024-09-17 02:10:16

unbeli 是对的 - 您需要 () 来调用函数,并且需要 * 来解压参数。然而,当你可以这样做时,就不需要使用deque

commandlist = [['A234', 5, True], ['B4242'], ['A234', 0]]

for command in commandlist:
   send_command(*command)

并且这将工作得很好。有关详细信息,请参阅解压参数列表

实际上,只有当您正在做一些想要消耗值的事情时才需要队列 - 比如说您希望命令列表在完成后为空。当然你也可以用列表做同样的事情:

q = [1,2,3,4]
while q:
   print q.pop(0)
print q

HTH

unbeli is right - you need the () to call the function, and you need * to unpack the arguments. However, there's no need for using deque when you can just do this:

commandlist = [['A234', 5, True], ['B4242'], ['A234', 0]]

for command in commandlist:
   send_command(*command)

and that will work perfectly fine. For more info, see unpacking argument lists.

Queues are really only necessary if you're doing something in which you want to consume the values - say you want your commandlist to be empty when you're done. Of course you could also do the same thing with a list:

q = [1,2,3,4]
while q:
   print q.pop(0)
print q

HTH

酒废 2024-09-17 02:10:16

你有没有尝试过:

send_command(CommandList.popleft()) # note the ()

Have you tried:

send_command(CommandList.popleft()) # note the ()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文