简体   繁体   English

读取当前迭代器值而不在Python中递增

[英]Reading current iterator value without incrementing in Python

I am writing a program with two distinct states (a reversible unit converter between feet and meters) and several functions within the program depend on the current state (the current value of an alternating itertools.cycle() iterator). 我正在编写一个具有两个不同状态的程序(在英尺和米之间的可逆单元转换器),程序中的几个函数取决于当前状态(交替迭代的循环(循环)迭代器)。 The user can call the reverse function to switch the current state and reverse the calculate conversion function. 用户可以调用反向功能来切换当前状态并反转计算转换功能。

Currently, I use next(currentstate) to return the next value of the iterator like this: 目前,我使用next(currentstate)返回迭代器的下一个值,如下所示:

self.currentstate = cycle(range(2))    

def reverse(self):
    if next(self.currentstate) == 0:
        self.feet_label.grid(column=2, row=2, sticky=tk.W)
    if next(self.currentstate) == 1:
        self.feet_label.grid(column=2, row=1, sticky=tk.W)

def calculate(self, *args):
    if next(self.currentstate) == 0:
        # convert feet to meters
    if next(self.currentstate) == 1:
        # convert meters to feet

Unfortunately, whenever a function is called and the if statement is computed, the cycle iterator is incremented by the next operator and the next call will yield something different. 不幸的是,无论何时调用函数并计算if语句,循环迭代器都会被下一个运算符递增,下一次调用将产生不同的结果。 The calculate function will likely be called many times in the same state, and so I'd like some way of retrieving the current value of the iterator without modifying or increment the operator. 计算函数可能会在同一个状态中多次调用,因此我想要一些方法来检索迭代器的当前值而无需修改或增加运算符。

def calculate(self, *args):
    if currentvalue(self.currentstate) == 0:
        # convert feet to meters
    if currentvalue(self.currentstate) == 1:
        # convert meters to feet

I have found a really ugly work-around involving calling next(currentvalue) twice in every if statement to reset the binary value. 我发现了一个非常丑陋的解决方法,涉及在每个if语句中调用next(currentvalue)两次来重置二进制值。 This may be a really awful way of writing a two state program like this, but it seems like there should be a way of doing this. 这可能是编写像这样的两个状态程序的非常糟糕的方式,但似乎应该有这样做的方法。 I am very new to Python and may also not fully understand the underlying theory of iterators. 我是Python的新手,也可能没有完全理解迭代器的基础理论。

Thanks 谢谢

It doesn't sound like you should be using an iterator here. 听起来你不应该在这里使用迭代器。 You should use something that has to EXPLICITLY change state. 你应该使用一些必须明显改变状态的东西。 It might be better to wrap this all in its own class. 将这一切包装在自己的类中可能更好。

class StateMachine(object):

    STATE_ON = 1
    STATE_OFF = 0  # this could be an enum maybe?

    def __init__(self, starting_state=0):
        self.state = starting_state

    def self.change_state(self):
        if self.state = self.STATE_ON:
            self.state = self.STATE_OFF
        else:
            self.state = self.STATE_ON

Now anywhere you use your state machine, you'll have to explicitly change the state. 现在,只要您使用状态机,就必须明确更改状态。

statemachine = StateMachine()

def calculate(*args):
    if statemachine.state == statemachine.STATE_ON:
        do_something
    if statemachine.state == statemachine.STATE_OFF:
        do_something_else

def switch_state(*args):
    do_something  # and...
    statemachine.change_state()

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

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