Use A Variable In A Function Without Passing As An Argument
Solution 1:
What you ask is possible. If a function doesn't re-bind (e.g, assign to) a name, but uses it, that name is taken as a global one, e.g:
def f():
print(foo)
foo = 23
f()
will do what you ask. However, it's a dubious idea already: why not just def f(foo):
and call f(23)
, so much more direct and clearer?!
If the function needs to bind the name, it's even more dubious, even though the global
statement allows it...:
def f():
global foo
print(foo)
foo += 1
foo = 23
f()
print(foo)
Here, the much better alternative would be:
def f(foo):
print(foo)
foo += 1
return foo
foo = f(23)
print(foo)
Same effect - much cleaner, better-maintainable, preferable structure.
Why are you looking for the inferior (though feasible) approach, when a better one is easier and cleaner...?!
Solution 2:
As @Alex Martelli pointed out, you can... but should you? That's the more relevant question, IMO.
I understand the appeal of what you're asking. It seems like it should just be good form because otherwise you'd have to pass the variables everywhere, which of course seems wasteful.
I won't presume to know how far along you are in your Python-Fu (I can only guess), but back when I was more of a beginner there were times when I had the same question you do now. Using global
felt ugly (everyone said so), yet I had this irresistible urge to do exactly what you're asking. In the end, I went with passing variables in to functions.
Why?
Because it's a code smell. You may not see it, but there are good reasons to avoid it in the long run.
However, there is a name for what you're asking:
Classes
What you need are instance variables. Technically, it might just be a step away from your current situation. So, you might be wondering: Why such a fuss about it? After all, instance variables are widely accepted and global variables seem similar in nature. So what's the big problem with using global variables in regular functions?
The problem is mostly conceptual. Classes are designed for grouping related behaviour (functions) and state (player position variables) and have a wide array of functionality that goes along with it (which wouldn't otherwise be possible with only functions). Classes are also semantically significant, in that they signal a clear intention in your mind and to future readers about the way the program is organized and the way it operates.
I won't go into much further detail other than to say this: You should probably reconsider your strategy because the road it leads to inevitably is this:
- 1) Increased coupling in your codebase
- 2) Undecipherable flow of execution
- 3) Debugging bonanza
- 4) Spaghetti code.
Right now, it may seem like an exaggeration. The point is you'd greatly benefit to learn how to organize your program in a way that makes sense, conceptually, instead of using what currently requires the least amount of code or what seems intuitive to you right now. You may be working on a small application for all I know and with one only 1 module, so it might resemble the use of a single class.
The worst thing that can happen is you'll adopt bad coding practices. In the long run, this might hurt you.
Post a Comment for "Use A Variable In A Function Without Passing As An Argument"