Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

Why does this print 4? Shouldn't it need a line inside the function like "global a", so "a" is in the function scope and not only in the main scope?

MIT 6.00 Intro Computer Science (OCW)
See more answers at
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Join Brainly to access

this expert answer


To see the expert answer you'll need to create a free account at Brainly

this print 4 because your code is very simple. in python you variable "a" is global Every time a function gets called, Python creates a new function frame, which contains the function's local variables and parameters. global a ----> 1.. never change within the function you create variable "a" applies only to the function
Because your function "f(x)" add the value of x to the value of a (which is always 1).
so simple 4... f send 3 as arguments to f which add 1 and return it to print and it prints it

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

For your answers I can see that I didn't express myself well. My question is related to the use of "global" and scopes. In that code we have: Main Scope: - a - f(x) f(x) Scope: - x So the object a is not in the function scope. How can the function use it without a "global a" line in the function?
The point is that works in the other way: 'a' is a global variable because is declared in the main program, then it is in the scope of the whole program (functions included). And you can use "global var" in the function definition to make the variable global and share that variable to use in other functions (even the main function).
I just ran some experiments and reached some conclusions that need confirmation. Objects in outermost scopes can always be used in inner scopes without using "global"? If f(x) and g(x) are too independent functions, to use in g an object created inside f we need to write "global object" in f?
That confirms what I suspected from recent results. Really thanks choutos and everyone else.

Not the answer you are looking for?

Search for more explanations.

Ask your own question