Python Functions

Sometimes, I see functions that call other functions that are defined later in Codecademy projects. Like this:

func1 = func2() + 1 # func2() is not defined yet def func2(): return 1 print(func1(), func2())

That is okay but with variable its not okay like this:

func1 = func2 + 1 # func2 is not defined yet func2 = 1 print(func1, func2) # Note: the variable names don't match what they actually are; I used them like that because to make the comparison between the 2 codebytes easier


Also, I sometimes see that functions modify global variables without a global statement in the Codecademy projects.
Like this:

global_var = None def global_var_changer(): global_var = 1 print(global_var)


What? This doesn’t change it but I have seen it before, look:


num_in_fibonacci = 9 function_calls = [] def fibonacci(num, memo): function_calls.append(1) if num < 0: print("Not a valid number") if num <= 1: return num elif memo.get(num): return memo.get(num) else: memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo) return memo[num] fibonacci_result = fibonacci(num_in_fibonacci, {}) print("Number {0} in the fibonacci sequence is {1}.".format(num_in_fibonacci, fibonacci_result)) print("Fibonacci function called {0} total times!".format(len(function_calls)))


What? function_calls is a global variable!

Um… no it isn’t:

Of course not. Why would it? It’s 2 separate variables that happen to have the same name declared in separate scopes.

function_calls is a name assigned to a list.

If you need further explanation, try reading the documentation, or reviewing the lessons.


The lessons didn’t teach this even in the scope lessons in the Learn Intermediate Python 3 course.

That’s why midliner used the word or.

What area in the documentation should I look at?

Scope and functions, perhaps?

No, it just says that functions can the global namespace by using global.

Yes? What else about functions and scope is troubling you? Midliner’s reply was pretty informative…

I think you should look at my previous posts.

Your previous posts aren’t helpful. Before Midliner’s reply, you’re asking about scope and functions. Then you asked about docs. I don’t know what it is you’re asking now. Did you not understand Midliner’s reply?

I don’t understand this:


When you are appending something to that list, you’re not actually reassigning anything; you’re just adding that list (since lists are mutable). That means you can access something in global scope.

What else(data types) can you change from a function from a global scope?

I pretty sure that you can do a similar thing for any mutable data type, although that would be a good thing for you to experiment with yourself…

1 Like

Ok so, do I open a workspace and set up some mutable data types and then declare a function which tries to change the data types and see if the data types are changed in the global namespace?

Sure, why not?

1 Like

Also, I want to try to convert it to C# but first I have to learn about methods in C#.

It may be worth mentioning that you didn’t call the function in this case, though it wouldn’t have accomplished the objective.


What do you mean? I don’t understand.

He means, in the code you posted:

Nowhere do you actually call the function global_var_changer(), so how do you expect it to do anything?

1 Like