I am confused on some parts of functions, like return, and scope and stuff, any advice?
Hello @waluigithetacostandg, I will explain
return and scope:
The return keyword basically hands the data back to the caller, which means when you call that function, it will essentially have the data in the return statement. The return also ends a function, so no code after it will be executed:
def function1(): return "Hello world!" print("Won't be printed")#This won't be printed, as it comes after a return #but is still inside a function
The phrase “Hello world!” can now be stored in any variable, or acted upon as though it were that data type:
somevariable = function1()#now stores "Hello world!" print(somevariable)#This prints "Hello world!", because that #is what the function returns
Scope is basically where a variable lives, and where it can be accessed. There are two main scopes in Python: function scope and global scope.
Global scope can be accessed anywhere in the program, while function scope can only be accessed in the function it was defined in:
var1 = "hi"#Has global scope. def function(): return var1#This returns "hi", as var1 is global scope, and can be accessed anywhere in the function
Local (or function) scope:
def somefunction(): var2 = "hey" return var2#returns "hey", because this variable was created #in this function print(var2) #throws an error, as you are outside the function #and this variable is locally scoped, which means it is not accessible outside of #its function
I hope this helps!
That was a prefect explanation. Thank You.
Global variables in Python aren’t quite as straightforward as they may seem. For example:
var1 = " you" def somefunction(): var2 = "hey" var1 = " there!" return var2 + var1 print(somefunction()) #prints hey there! print(var1) #what will this print?
var1 = " you" def somefunction(): var2 = "hey" var1 += " there!" #throws UnboundLocalError: local variable 'var1' referenced before assignment return var2 + var1 print(somefunction())
Trying to alter what seems to be a global variable doesn’t work in the above case.
var1 inside the function is not the globally scoped
var1 declared outside of the function. It is a new function scoped variable that happens to have the same name. In order to reference the global
var1 we have to tell the computer that’s what we’re doing using the
var1 = " you" def somefunction(): var2 = "hey" global var1 var1 += " there!" return var2 + var1 print(somefunction()) #hey you there! print(var1) # you there!
But please, don’t ever do this:
global doesn’t exist for a few years.
See my discussion of this here.
Also, just for clarification, you don’t need to use
global to access a global variable inside of a function. That is only if you want to alter the global variable inside your function.
Right. Changing my first example will show that:
var1 = " you" def somefunction(): var2 = "hey" return var2 + var1 print(somefunction()) #prints hey you
I don’t* like to see or use the
global keyword personally. I was just pointing out that what seems like it should be easy enough to do given some other languages, altering a global variable from within a function isn’t completely straightforward.
*Edited 5/20/2020 to clarify. I had left out the word
It’s totally valid to use
global in small programs or if you know exactly what you’re doing. I just try to caution all beginners against it because it’s too easy to get used to using it and then when you’re writing a larger program it comes back to bite you in the butt.
Also, I think beginners tend to use it as a crutch rather than using more appropriate structures (such as dictionaries, etc., which are mutable in place).
I totally agree. I like to keep everything as tightly scoped as possible.