【问题标题】:Why does one of my variables doesn't need declaration while the other one does?为什么我的一个变量不需要声明,而另一个需要声明?
【发布时间】:2014-11-23 19:09:09
【问题描述】:

此代码运行没有错误。但是在函数find_available_filenumber 中,变量render_folder 没有被声明。所以我的问题是为什么这不会产生错误?

如果我删除 full_filename 作为参数,我会收到错误:

UnboundLocalError: local variable 'full_filename' referenced before assignment.

我不明白为什么render_folder 也不会发生这种情况,在下面的代码示例中:

import bpy
import os

#Functions
def find_available_filenumber(full_filename):
    file_number = 1

    while os.path.exists(render_folder + "\\" + full_filename):
        file_number += 1
        full_filename = create_full_filename(filename, file_number)

    return file_number

def create_full_filename(filename, file_number):
    file_extension = ".avi"
    full_filename = filename + "_" + str(file_number) + file_extension    

    return full_filename

#Paths and names
project_folder = "F:\\06_MotionPath_Dev\\"
os.chdir(project_folder)

render_folder = "Render\\QuickRenderAddon"
filename = bpy.context.scene.name  #Returns "QuickRenderAddon"
full_filename = create_full_filename(filename, 1)
filepath = render_folder + "\\" + full_filename

available_number = find_available_filenumber(full_filename)
print("Avail nmb: " + str(available_number))

【问题讨论】:

    标签: python function variables python-3.x scope


    【解决方案1】:

    是的,经典的“在分配前引用”

    我写了一些示例代码来展示发生了什么。

    test = "toast!"
    toast = "test!"
    
    def func():
        print test
        print toast
    
    func()
    

    上面的输出是

    toast!
    test!
    

    这表明我们可以读取全局变量,但是写入它们呢?我不希望“吐司”成为“测试!”不再,而是“面包+烤面包机!”。让我们把这个写出来。

    test = "toast!"
    toast = "test!"
    
    def func():
        print test
        toast = "bread+toaster!"
        print toast
    
    func()
    print toast
    

    这个输出

    toast!
    bread+toaster!
    test!
    

    您会注意到我们能够打印本地分配的变量,但全局变量没有改变。现在,让我们看另一个例子。

    test = "toast!"
    toast = "test!"
    
    def func():
        print test
        print toast
        toast = "bread+toaster!"
        print toast
    
    func()
    print toast
    

    这会抛出错误

    UnboundLocalError: local variable 'toast' referenced before assignment
    

    为什么?因为您稍后将变量“toast”声明为局部变量。 Python 注意到这一点并停止代码以防止错误。您稍后设置“toast”不是更改全局变量,而是在名为“toast”的函数中声明一个本地变量。

    你怎么能解决这个问题?

    首先是在你的函数中添加一个全局子句

    test = "toast!"
    toast = "test!"
    
    def func():
        global toast
        print test
        print toast
        toast = "bread+toaster!"
        print toast
    
    func()
    print toast
    

    这个输出

    toast!
    test!
    bread+toaster!
    bread+toaster!
    

    您也可以将代码修改为类结构。

    class bread():
        def __init__(self):
            self.test = "toast!"
            self.toast = "test!"
    
        def func(self):
            print self.test
            print self.toast
            self.toast = "bread+toaster!"
            print self.toast
    
    b = bread()
    b.func()
    

    在我看来,类是最好的解决方案,因为它会增加模块化并帮助您减少意大利面条式代码。

    【讨论】:

      【解决方案2】:

      编辑:忽略我,我在手机上,所以我没有正确阅读。

      错误状态“在分配之前引用”。换句话说,您是在向该变量写入值之前尝试从该变量中读取值。

      我感觉这个错误是在您的 while 条件中引起的,因为您在写入 while 循环体之前检查了该值。

      【讨论】:

        【解决方案3】:

        此代码运行没有错误。但是在函数 find_available_filenumber 中没有声明变量 render_folder。所以我的问题是为什么这不会产生错误?

        这是因为 render_folder 是在调用 find_available_filenumber 时声明的,即使在定义函数时它没有声明。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多