Why does one of my variables doesn't need declaration while the other one does?

随声附和 提交于 2019-12-07 17:39:28

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.

Edit: Ignore me, I'm on mobile, so I didn't properly read.

The error states "referenced before assignment". In other words, you are trying to read a value from that variable before you have written a value to it.

I have a feeling this error is being cause in your while conditional, as you are checking the value before writing to it in the body of the while loop.

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?

This is because render_folder is declared at the time find_available_filenumber is called, even though its not declared when your function is defined.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!