Nested functions

Okay, so I’m doing flash cards to help quiz myself.

One of my flash cards are below.

The answers confused me.

So I asked ChatGpt and it came back with my thought process and my own answer of: It isn’t correct.

But I then put it in my IDE and it came back with what the flash card says.

What is going on? Which answer is right and why?



Screenshot_20231127_074319_Pydroid 3

Note - coding isn’t about memorization so you don’t need flash cards

Also note - GPT has no way to know if it’s totally lying to you

I don’t think your note on the card is quite right. Inside of the inner function you use the ‘global’ keyword to access and modify the variable a in the global scope, but the outer function still has its own local version of the variable a.

Hi @EvanDiTech,

So actually ChatGPT is wrong in this instance. Your outer function is creating a brand new variable called a since it does not use the global a statement that will pull the global variable into the outer function. That is why it is 0 in the “outer2” print but it is 2 in the “main2” print.

Check out this, for example, where I add global a to the outer function:

def outer():
    global a
    def inner():
        global a
        a = 2
        print("inner:", a)

    a = 0
    print("outer1:", a)
    inner()
    print("outer2:", a)

a =  1
print("main1:", a)
outer()
print("main2:", a)

It then has the output you expect because it is modifying the global variable a, not the local variable a created within the outer function that is in your original code.

1 Like

You might find a tool like this useful to step through the program and see what’s happening:

https://pythontutor.com/python-compiler.html#mode=edit

https://cscircles.cemc.uwaterloo.ca/visualize

2 Likes

I appreciate the answer and your example! Thank you!

It’s not about memorization per say, more so the method of studying called “active recall and spaced repetition”. Both of which I’ve used to pick up a N3 level of Japanese and Russian, and sometime down the line, Spanish.

It’s a scientifically backed method of learning that trains the brain to look at certain patterns and easily differentiate between them, amoung other benefits!

I work 13 hours days, so I have only so much time to sit down and code and being able to break down problems by doing my own cards or someone else’s debugging on the fly helps a ton with being able to recall the information at any time I might need it!

2 Likes

A programming language and a natural language work completely differently in the brain.

Spaced repetition is great at helping you recall data, like vocab or medical facts. That’s not really how programming learning works though.

Possibly! Everyone has their own opinion, and I’m going off past experience and that of several youtubers who are also in the industry, who have also used the method, as it brought me back to using it with code.

There are even plugins that work with VSCode and Anki cards.

But; I’m not using it to learn theory! I’m learning it to get used to seeing code and be able to quickly deconstruct code into smaller parts to be understandable to myself.

It’s worked for me so far, and that’s what’s best for anyone studying anything.

1 Like

Coding isn’t really about remembering a ton of stuff, but if you like it, do it.

1 Like

Absolutely, I get that. It won’t help me with problems solving and the like. You can’t just memorize a program.

I get where you’re coming from.

I’ve done research on my end! Everyone has a different opinion it, and even the same goes for if it’s similar to language learning.

Even those on YouTube, one guy being from Google says he uses anki, a guy from Adobe says it’s a waste of time.

Even Google and Reddit don’t agree with themselves.


I “memorize” structure and “Syntax”/grammar from reading it, but flash cards won’t help you write it.

I’m absolutely better at reading code and understanding what’s going on, than I am at sitting down and writing it out due to not having as much practice as the time between work and code can be days to weeks.

I absolutely look up more than my fair share of concepts when making anything.

When using Java to mod minecraft I had to look a ton syntax and methods up as when I was making games with unity C# and simply dragging files to parent them to a game object was extremely different. Flash cards with anki did a ton of reference work for me there.

Everyone has something that works for them! This just has worked for me.

Biologically, different areas of your brain activate with a natural language vs a programming language. It’s not the same thing.

1 Like

I’ll give that a read when I have the chance, I appreciate it!

That may be. All I could possibly tell you is outside game dev I’ve had very limited code exposer and things like Global and Nonlocal are new to me and I’ve only seen them through things like flash cards.

And now through reading and spaced repetition, if I ever have use of them, I know what they do and will be able to work off that knowledge.

They may go into different parts of the brain, but I got the results I needed for now and can build off those with more practical experience when the need arises! Which suits my own needs. And I figure that’s what coding is about, using it for things that are needed to be done.

If you’re happy with using spaced repetition to memorize syntax, go nuts.

Your IDE is likely showing the correct result based on the code execution. Trust the IDE output for accuracy.