Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过循环中的函数返回值更新多个(局部)变量_Python_Python 3.x - Fatal编程技术网

Python 通过循环中的函数返回值更新多个(局部)变量

Python 通过循环中的函数返回值更新多个(局部)变量,python,python-3.x,Python,Python 3.x,假设我有这个(当前返回较少)函数: def codepoint_convert(text, offset): codepoint = text[offset] if codepoint <= 0x01: output = "\n" elif codepoint >= 0x09 and codepoint <= 0x12: # digits output = chr(0x30 + (codepoint-0x09))

假设我有这个(当前返回较少)函数:

def codepoint_convert(text, offset):
    codepoint = text[offset]

    if codepoint <= 0x01:
        output = "\n"
    elif codepoint >= 0x09 and codepoint <= 0x12: # digits
        output = chr(0x30 + (codepoint-0x09))
    elif codepoint >= 0x13 and codepoint <= 0x2C: # uppercase letters
        output = chr(0x41 + (codepoint-0x13))
    elif codepoint >= 0x2D and codepoint <= 0x46: # lowercase letters
        output = chr(0x61 + (codepoint-0x2D))
    elif codepoint >= 0x47 and codepoint <= 0x62: # multi-byte codepoints
        offset += 1
        codepoint = (codepoint << 8) + text[offset]
        output = cp_dict[codepoint]
    else:
        print("Invalid codepoint at 0x%08X" % offset)

    offset += 1
我以前使用过两种方法:

1.
def convert_码点(文本、偏移、输出):
#答:请参阅第一个代码段
#B:连接到“输出”(+=)而不是赋值(=)
返回[偏移,输出]
def main():
text=“\x0A\x0B\x0C\x01”
偏移量=0
输出=“”
当偏移量
2.
offset=0#全局变量
def convert_代码点(文本、偏移):
全局偏移
#答:请参阅第一个代码段
返回输出
def main():
text=“\x0A\x0B\x0C\x01”
输出=“”
当偏移量
对我来说,第一种方法令人困惑,因为它似乎取代了
偏移量
输出
变量,而不是更新它们,因为它使用
=
而不是
+=
(无论如何,我似乎无法在Python 3.4.2中的列表赋值中使用
+=
,因为它抛出了一个
语法错误(“用于增强赋值的非法表达式”)。而且使用列表作为返回值似乎也不那么方便端口

我对第二种方法的不满是它使用了一个全局变量。我希望它能够调用
convert\u codepoint()
(例如,如果脚本作为模块导入)无需定义全局变量。
offset
变量可能也需要从
main
函数重新初始化,这样可能会变得混乱


我可以尝试任何其他方法,以一种简洁明了的方式在本地更新变量?

为什么不使用一个函数返回下一个输出和偏移量,然后将下一个输出元素附加到输出列表中:

def get_next_output_offset_pair(text, offset):
  #A: Adapt first code snippet
  return [next_offset, next_output]

def main():
   text = "\x0A\x0B\x0C\x01"
   offset = 0
   output = ''
   while offset < len(text):
     offset, next_output = get_next_output_offset_pair(text, offset)
     output.append(next_output)

我认为您的第一个解决方案非常清楚,但是您的代码应该对您有直观的意义,而不会给下一个维护人员带来困难。

我认为您的第一个方法是正确的。通过将函数重命名为
update\u codepoint
get,可以明确添加变量的事实_更新了_输出
或类似的东西。
def convert_codepoint(text, offset, output):
    # A: see first code snippet
    # B: concatenate to "output" (+=) instead of assigning (=)
    return [offset, output]

def main():
    text = "\x0A\x0B\x0C\x01"
    offset = 0
    output = ''
    while offset < len(text):
        offset, output = convert_codepoint(text, offset, output)
offset = 0 # global variable

def convert_codepoint(text, offset):
    global offset
    # A: see first code snippet
    return output

def main():
    text = "\x0A\x0B\x0C\x01"
    output = ''
    while offset < len(text):
        output += convert_codepoint(text, offset)
def get_next_output_offset_pair(text, offset):
  #A: Adapt first code snippet
  return [next_offset, next_output]

def main():
   text = "\x0A\x0B\x0C\x01"
   offset = 0
   output = ''
   while offset < len(text):
     offset, next_output = get_next_output_offset_pair(text, offset)
     output.append(next_output)
      next_offset, next_output = get_next_output_offset_pair(text, offset)
      output.append(next_output)
      offset = next_offset