课程: Programming Concepts for Python

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Local and global variables

Local and global variables

- While I'm in the process of cooking something, I like to keep the things I'm working with close at hand, in my local area. Functions do the same thing when they execute by keeping their input parameters and any variables which are created inside of the function within that function's local scope. When I want to use a specific resource, I'll look for it first in the local area. So if I want to add some bacon to my omelet, I'll look for it nearby. Here it is, so I'll add that. Now, if I want to add some cheese to my omelet, I'll start by looking for it in my local scope. But I don't seem to have any cheese here, so I'll expand my search to look in the global scope. And in this case, that's my kitchen, so I'll go get some cheese from the fridge. Whoa. - Ooh, bacon. Can I have some? - Oh, no, no, no, no, you can't touch that bacon. That bacon is my input ingredient, which means it's in my local scope. Only I can use…

内容