Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

David_Novo

Why does this print 4? http://dpaste.com/779443/ 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?

  • one year ago
  • one year ago

  • This Question is Closed
  1. lopus
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  2. choutos
    Best Response
    You've already chosen the best response.
    Medals 1

    Because your function "f(x)" add the value of x to the value of a (which is always 1).

    • one year ago
  3. anzaq
    Best Response
    You've already chosen the best response.
    Medals 0

    so simple 4... f send 3 as arguments to f which add 1 and return it to print and it prints it

    • one year ago
  4. David_Novo
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • one year ago
  5. choutos
    Best Response
    You've already chosen the best response.
    Medals 1

    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).

    • one year ago
  6. David_Novo
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • one year ago
  7. David_Novo
    Best Response
    You've already chosen the best response.
    Medals 0

    That confirms what I suspected from recent results. Really thanks choutos and everyone else.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.