This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python code to get current function into a variable?
is there a convenient way to get to pointer of current function?
for example:
current_func_ref = None
def func():
current_func_ref = ___ #something need to fill
do something..
Ok this is a bit cheeky, not very generic like you might want, but here it is anyway:
current_func_ref = None
def func():
global current_func_ref
current_func_ref = func
Related
This question already has answers here:
Calling variable defined inside one function from another function
(6 answers)
Closed 2 years ago.
I have open source web server written in Python. Inside the server I have a wrong_checker() function which takes a value and checks it against real_value. I can access this function.
def wrong_checker(value_of_check):
return (value_of_check == "" or (not value_of_check and real_value)) \
or value_of_check != real_value
Now I want to write function find_value() to find and print the value of real_value.
def find_value():
???????
Well, normally you should write a comparer funciton
def compare(value_a, value_b):
#compare stuff and return 1, 0 or -1
#1 for value_a greater, 0 for equal and -1 for value_b greater
Then you are more flexible.
This question already has answers here:
Function closure vs. callable class
(6 answers)
Closed 2 years ago.
Is either of the two styles preferred or more "pythonic" for creating closures (edit: "closure-like objects")?
def make_doer(closed_var):
def doer(foo):
pass # Do something with closed_var and foo
return doer
class Doer:
def __init__(self, closed_var):
self._closed_var = closed_var
def __call__(self, foo):
pass # Do something with self._closed_var and foo
The only differences I can tell are that the former is a tiny bit shorter but the second has an advantage in that the docstring for the resulting function (__call__ in the second case) is less nested/hidden. Neither seems like a huge deal, anything else that would tip the balance?
Closures have to do with maintaining references to objects from scopes that have passed, or from earlier scopes.
Here is the simplest example of the form a Closure takes:
def outer():
x=7
def inner(y):
return x + y
return inner
i = outer()
This question already has answers here:
What does it mean when the parentheses are omitted from a function or method call?
(6 answers)
Closed 4 years ago.
So I got stuck on something like this (a simplified version)
class Node:
def __init__(self):
self.left= None
self.cost = 0
def change(self):
if self.left is not None:
self.left.cost=self.cost+1
self.left.change
data=[]
for i in range(10):
data.append(Node())
if i>0:
data[i].left = data[i-1]
data[8].change()
print(data[2].cost) #0
I want data[2].cost to have changed, but it rollbacks. Can I make it works without skipping recursion? (In full version I actually keep a two-dimensional array of nodes that have four pointers, so making an iteration suck.)
You forgot () when you call your change method.
def change(self):
if self.left is not None:
self.left.cost=self.cost+1
self.left.change()
output:
6
Obviously you want to call change but you didn't. You just refered to the function object and did nothing with it.
Just change self.left.change to self.left.change() to call it
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 5 years ago.
Why does this code return None?
def html_list(inputs_list):
print("<ul>")
for html in inputs_list:
html = ("<li>" + html + "</li>")
print(html)
print("</ul>")
bam = ['boom','bust']
print(html_list(bam))
Your function has print calls within it, but does not return anything. If a function completes without returning, it really returns None.
This means that if you call the function inside a print statement, it will run, do the prints within the function, but return None which will then get passed into the print() function - so this is why you get the intended output and then a "None" at the end.
You can also see this through a more simple example:
>>> def f():
... print("inside f")
...
>>> print(f())
inside f
None
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 4 years ago.
I'm trying to store the result of random.random into a variable like so:
import random
def randombb():
a = random.random()
print(a)
randombb()
How can I properly get a?
Generally, you return the value. Once you've done that, you can assign a name to the return value just like anything else:
def randombb():
return random.random()
a = randombb()
As a side note -- The python tutorial is really great for anyone looking to learn a thing or two about the basics of python. I'd highly recommend you check it out. Here is the section on functions: https://docs.python.org/2/tutorial/controlflow.html#defining-functions ...
You can make it a global variable if you don't want to return it. Try something like this
a = 0 #any value of your choice
def setavalue():
global a # need to declare when you want to change the value
a = random.random()
def printavalue():
print a # when reading no need to declare
setavalue()
printavalue()
print a
You can simply store the value of random.random() as you are doing to store in a, and after that you may return the value
import random
def randombb():
a = random.random()
print(a)
return a
x= randombb()
print(x)