This question already has answers here:
Get function callers' information in python
(3 answers)
How to get the caller's method name in the called method?
(11 answers)
Closed 5 years ago.
I've been thinking about this question for a while, and I can't seem to find any related questions to it, probably because I don't know the proper terminology for what I'm looking for.
Is there any condition for an if-else statement in a function that is reliant on which other function is doing the calling? Such as:
def FUNC():
if func1 called FUNC:
statement
elif func2 called FUNC:
statement
def func1():
FUNC()
def func2():
FUNC()
I'm not sure what purpose there would be behind doing this, but I just wanted to know if it was an available option. Thank you.
Solution: pass an argument.
def FUNC(who_called_me):
if who_called_me == 'func1':
# statement
elif who_called_me == 'func2':
# statement
def func1():
FUNC(who_called_me='func1')
def func2():
FUNC(who_called_me='func2')
The argument (who_called_me) does not have to be of type string, but can also be an integer, boolean or an object.
Related
This question already has answers here:
How do I forward-declare a function to avoid `NameError`s for functions defined later?
(17 answers)
Closed 3 years ago.
Simple python script is giving error, what is wrong?
var ="first variable"
myfun(var)
def myfun(var):
print(var)
Error -> NameError: name 'myfun' is not defined
This thing is quite obvious though. Python reads the code line by line and not like C.
Just switch your two blocks i.e. definition of function and calling it.
var ="first variable"
def myfun(var):
print(var)
myfun(var)
This should be good to go.
When python interpreter sees the statement myfun(var), the name myfun is not defined yet. You need to move this line after your function definition.
This question already has answers here:
Python overwriting variables in nested functions [duplicate]
(4 answers)
Closed 6 years ago.
I need to define a variable within a function and use it in a function that is within the original function. For example,
def go_to_next_function():
globvar = 0
def add_one_to_globvar():
global globvar
for i in range(10):
globvar += 1
print(globvar)
add_one_to_globvar()
go_to_next_function()
Now I know if globvar is defined outside all of the functions then it would work, but I want it to be defined inside the go_to_next_function() function. Any ideas? Thanks.
If you are using python3.x, you can use the nonlocal keyword rather than global and your function should work.
If you're on python2.x, you're stuck with old hacks like putting the value in a container and mutating that. It's an ugly hack (which is why nonlocal was added in the first place):
def go_to_next_function():
closure_var = [0]
def add_one():
closure_var[0] += 1
print closure_var[0]
add_one()
go_to_next_function()
This question already has answers here:
Scoping in Python 'for' loops
(8 answers)
Closed 7 years ago.
Consider this example:
for iter in xrange(10):
myvar = iter
print myvar
# 9
Here myvar is clearly outside the loop? But it is still accessible.
If this is Perl, it will throw an error.
What's the reason behind such feature in Python?
Is it harmful? What's the best practice then, to declare a variable before looping?
There is no new scope created by the for loop (Ruby also behaves the same way). This may be surprising if you are coming from a language that creates new scopes for blocks.
I don't believe it's harmful as long as you know the rules.
If your functions and methods are so large that you have trouble keeping track, then your functions and methods are too large.
Think of it as if you're doing this:
my_var = 0
my_var = 1
my_var = 2
...
my_var = 9
print my_var
This is basically what the iteration will look like, there is no reason why my_var would not be accessible from within your current method / scope.
This question already has answers here:
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 8 years ago.
when working with python, it bothered me that while obj.method() is perfectly fine, method(obj) isn't allowed. So I figured I'd try to write some code to fix that. I came up with the next:
def globalclassfuncs(defobj):
for i in inspect.getmembers(defobj, predicate=inspect.ismethod):
def scope():
var = i[0];
setattr(sys.modules[__name__], i[0], lambda obj, *args: getattr(obj, var)(*args));
scope();
However, there's something weird with this. When I remove def scope(): and scope(), so that it'll run without a function definition in the for loop, or when I change the getattr() function to use i[0] directly instead of through var, somehow all new defined functions point to the last defined function instead of the function they should point to. Why does this behaviour change so much on such small changes in the code?
Seems like a case of late binding closure
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Understanding Python decorators
What function does the "class decorator"/"method decorator" (#) serve? In other words, what is the difference between this and a normal comment?
Also, what does setter do when using #previousMethod.setter before a method? Thank you.
#decorator
def function(args):
#body
is just syntactic sugar for:
def function(args):
#body
function = decorator(function)
That's really it.
As you see, the decorator gets called, so it's by no means a comment.