【问题标题】:Not able to print global variables无法打印全局变量
【发布时间】:2021-02-01 15:40:48
【问题描述】:

调用函数后,我无法打印最终语句。我从这里尝试了其他解决方案,但似乎无法让 read_size/clock_rate 真正成为全球性的。

def get_config():
    global clock_rate
    global read_size
    role = input("Will the Sync Link be used as downlink or uplink? Type \"downlink\"
                 "for downlink, \"uplink\" for uplink: ")
    if role == "downlink":
        clock_rate = 10240 # Convolutional encoding always disabled when sending to EDU
        print("Configured for downlink mode... Clock rate =",clock_rate,"bits per second.")
    if role == "uplink":
        CE_enabled = input("Is convolutional encoding enabled on the EDU? "
                           "1 is enabled, 0 is disabled: ")
        if CE_enabled == 1:
            read_size = 512 # When convolutional encoding is enabled, the EDU receives
                            # a 512 byte CADU frame.
            print(read_size)
            clock_rate = 20480
        if CE_enabled == 0:
            read_size = 256 # When convolutional encoding is disabled, the EDU receives
                            # a 256 byte CADU frame.
            clock_rate = 10240
        else:
            print("Invalid input")

get_config()
print("Initiating transmit/receive... Read size =", read_size,
      "bytes. Clock rate =", clock_rate, "bits per second.")

【问题讨论】:

  • 您需要在函数范围之外定义clock_rateread_size。为什么不让你的函数返回 read_sizeclock_rate 呢?
  • 我尝试在函数外部定义变量,但仍然无法让 read_size 工作。我可以这样做,但这需要我重写更多的函数。
  • 我强烈建议重写更多函数
  • 函数的执行可能会也可能不会为这两个变量赋值——所以如果你要在调用后引用它们的值,你需要确保它们存在并且在调用它之前有一些默认值它。

标签: python scope global


【解决方案1】:

即使忽略对全局变量的依赖,您也没有在所有可能的情况下定义 clock_rateread_size。执行以下操作:

def get_config():
    clock_rate = None
    read_size = None
    role = input("Role?")
    if role == "uplink":
        clock_rate = 10240
    elif role == "downlink":
        ce_enabled = input("Convolutional encoding enabled?")
        if ce_enabled == "0":
            read_size = 256
            clock_rate = 10240
        elif ce_enabled == "1":
            read_size = 512
            clock_rate = 20480
        else:
            raise ValueError("Invalid response for convolutional encoding")
    else:
        raise ValueError("Invalid role")
    return clock_rate, read_size


clock_rate, read_size = get_config()

print("Initiating transmit/receive... Read size =",read_size,"bytes. Clock rate =",clock_rate,"bits per second.")

您可以为每个变量提供默认值(除了先前分配的None),而不是在每种情况下都引发错误。

【讨论】:

    【解决方案2】:

    您需要在使用 global 关键字修改其范围之前声明时钟速率和 read_size 变量,以便您可以更改它们。

    您还需要一个 elif CE_enabled==0,否则如果您输入 1,您总是会得到无效的输入。

    见下文:

    clock_rate=None
    read_size = None
    
    def get_config():
        global clock_rate
        global read_size
        role = input("Will the Sync Link be used as downlink or uplink? Type \"downlink\" for downlink, \"uplink\" for uplink: ")
        if role == "downlink":
            clock_rate = 10240 # Convolutional encoding always disabled when sending to EDU
            print("Configured for downlink mode... Clock rate =",clock_rate,"bits per second.")
        if role == "uplink":   
            CE_enabled = int(input("Is convolutional encoding enabled on the EDU? 1 is enabled, 0 is disabled: "))
            if CE_enabled == 1:
                read_size = 512 # When convolutional encoding is enabled, the EDU receives a 512 byte CADU frame
                print("Read size:", read_size)
                clock_rate = 20480
            elif CE_enabled == 0:
                read_size = 256 # When convolutional encoding is disabled, the EDU receives a 256 byte CADU frame
                clock_rate = 10240
            else:
                print("Invalid input")
    
    get_config()
    
    print("Initiating transmit/receive... Read size =",read_size,"bytes. Clock rate =",clock_rate,"bits per second.")
    

    【讨论】:

    • 这根本不是真的,看我的回答。
    • 谢谢你,我用过这个,它成功了。使用您的原始代码,我使用的 API 出现错误,但是在将 clock_rate = None 和 read_size = None 放入 get_config() 之后,我修复了它。非常感谢您的帮助。
    【解决方案3】:

    我将把它作为答案而不是评论来阻止任何混乱的发生。要使用全局变量,您不必必须在全局范围内初始化它们。以下代码将完美运行:

    def some_function():
        global some_global
        some_global = "Hello, world!"
    
    some_function()
    print(some_global)
    

    全局变量必须在外部范围内初始化。相反,它们可以随时在任何范围内定义。唯一的限制是:它们必须在访问之前定义。这就是你的代码有问题。

    在“下行链路”模式下,您的函数没有定义read_size,而在“上行链路”模式下,您将输入与01 进行比较,它们是整数,因此这些表达式永远不会产生在True。这意味着,到达else 块,告诉用户输入无效。在这种情况下,read_sizeclock_rate 都不会在最终打印语句之前定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多