为什么我的一个变量不需要声明而另一个呢? [英] Why does one of my variables doesn't need declaration while the other one does?

查看:112
本文介绍了为什么我的一个变量不需要声明而另一个呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  UnboundLocalError:赋值前引用的局部变量'full_filename'。 

我不明白为什么在 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(文件名,文件编号)

返回文件编号

def create_full_filename(文件名,文件编号):
file_extension =.avi
full_filename = filename +_+ str(file_number)+ file_extension

返回full_filename

#路径和名称
project_folder =F:\\06_MotionPath_Dev\\\
os.chdir(project_folder)

render_folder =Render \\QuickRenderAddon
filename = bpy。 context.scene.name#返回QuickRenderAddon
full_filen ame = create_full_filename(filename,1)
filepath = render_folder +\\+ full_filename

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


解决方案

在分配之前



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

  test =敬酒! 
toast =test!
$ b $ def func():
print test
print toast

func()

上面的输出是

  toast! 
测试!

这表明我们可以读取全局变量,但写入它们又怎么样?我不想'烤面包'成为'测试',而是'面包+烤面包机!'。让我们写出来。

  test =toast! 
toast =test!

def func():
print test
toast =面包+烤面包机!
打印toast

func()
打印toast



<这个输出

  toast! 
面包+烤面包机!
测试!

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

  test =toast! 
toast =test!

def func():
print test
print toast
toast =面包+烤面包机!
打印toast

func()
打印toast

这将抛出错误

  UnboundLocalError:赋值前引用的局部变量'toast'

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



你如何解决这个问题?



第一种是在你的函数中添加一个全局子句

  test =敬酒! 
toast =test!
$ b $ def func():
全球吐司
打印测试
打印吐司
吐司=面包+烤面包机!
打印toast

func()
打印toast



<这个输出

  toast! 
测试!
面包+烤面包机!
面包+烤面包机!

您也可以将您的代码修改为类结构,如此。

  class bread():
def __init __(self):
self.test =toast!
self.toast =测试!

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

b = bread()
b.func()

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

This code runs without errors. But in the function find_available_filenumber the variable render_folder isn't declared. So my question is why this doesn't produce an error?

If I remove full_filename as a parameter, I get the error:

UnboundLocalError: local variable 'full_filename' referenced before assignment.

I don't understand why this doesn't also happen with render_folder, in my code example below:

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))

解决方案

Ah yes the classic "Referenced before Assignment"

I wrote some example code to show what is going on.

test = "toast!"
toast = "test!"

def func():
    print test
    print toast

func()

The output of the above is

toast!
test!

This shows that we can read global variables, but what about writing to them? I don't want 'toast' to be 'test!' anymore, but rather 'bread+toaster!'. Let's write this out.

test = "toast!"
toast = "test!"

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

func()
print toast

This outputs

toast!
bread+toaster!
test!

You'll notice that we were able to print the locally assigned variable, but the global variable did not change. Now, let's look at another example.

test = "toast!"
toast = "test!"

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

func()
print toast

This will throw the error

UnboundLocalError: local variable 'toast' referenced before assignment

Why? Because you're later declaring your variable 'toast' as a local variable. Python notices this and stops the code to prevent errors. You setting 'toast' later is not changing the global variable, but rather declaring a local one within the function called 'toast'.

How can you fix this?

The first would be to add a global clause inside your function

test = "toast!"
toast = "test!"

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

func()
print toast

This outputs

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

You can also modify your code into a class structure, as such.

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()

In my opinion, classes are the best solution as it will increase modularity and help you reduce spaghetti code.

这篇关于为什么我的一个变量不需要声明而另一个呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆