A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Question on PSs 5 and 6: Why are the functions passing word_list around?

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Specifically: word_list is defined globally--why do functions need to get it as an argument? Is it set up that way just to provide for the possibility of using a different list?

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think it is a good design pattern. It makes the functions that use the word list independent of the word list being used. You'd be amazed at how much trouble you can get into by having too many global variables that you have to keep track of. I don't think the "different list" idea would hold water, because you could meet that objective by just supplying a different filename instead of "words.txt". I think the purpose is to minimize the number of global variables, which have a well-earned bad reputation.

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yeah, that's what it looked like to me, but I wanted to make sure I wasn't missing anything. Thanks!

  4. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    It's definitely a good idea to do things that way. It's declared as a global variable more as a matter of simplicity than anything else. The starting point that defines the initial word list could just as easily have been in a function that was then invoked as the only thing happening at the top level; e.g.: def doProgram: word_list = [] # do stuff with word_list doProgram()

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    That makes sense--If I understand correctly, you're saying that if it was declared in the function the way you have it, it would be local to that function and thus it would need to be passed explicitly to every function that needed it. Your explanation makes it clearer. Thanks!

  6. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Correct. And in most applications, you write everything in other classes and functions, and the only thing at the very top level is a call to a function that kicks it all off.

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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.