Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How can I refer to a previous function in a new function in python 2.7.
For instance, say if I want to display the result which function1 calculated in function2. How would I go about this?
wouldn't it be:
def func_one():
return 2 + 2
def func_two():
x = func_one()
print x
func_two()
#output: 4
You need to understand the flow of your program. A function doesn't run until it is called. When it is called, it may return a value (if you say it calculates something, it should return that). That return value is available to the caller of the function, but not to any other functions. So function2 cannot use it. Unless it is passed that value as an argument, that is:
def function1():
return 42
def function2(value):
print('The value is %d' % value)
x = function1()
function2(x)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
def twoDim(foo):
two_d= []
new = []
for i in range(0,9):
for j in range(0,9):
new.append(foo)
two_d.append(new)
twoDim(0)
print(twoDim)
twoDim is name for a function. You want to see result of its call
print(twoDim(0))
or
result = twoDim(0)
print(result)
Your function also doesn't return anything currently so any result of its call will be None. You need to add return statement with whatever variable you want to be the output of the function.
Your function doesn't have a return statement which returns a value when the function is called so you can add a return statement which returns the value you expect the function to have when using it with the print function.
Also, keep in mind that using only the name of the function without parenthesis like you did in your code doesn't call the function but returns the memory location of your function object.
twoDim is the function name.
So, instead of print(twoDim), you should write print(twoDim(0)) which means calling the function and printing its return value.
In your case, twoDim function doesn't have a return value, so the correct print statement would also not print anything.
If you intend to print two_d and new, you can do that in the function itself and then just call the function OR return something from the function.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to know the assigning to variable more specifically. My code:
def fun():
return 5
var=fun()
print(var)
var=fun
print(var)
Is there any difference in the both or are they the same? I use Python 3.
var = fun() calls the function fun. The returned value (in this case, 5) is then assigned to var.
var = fun assigns the function fun refers to, to var. You can now call the function by doing either fun() or var(). Two different names for the same function.
This is what people mean when they say python has 'first-class functions' - they can be treated like any other variable, as opposed to the special treatment they get in languages like Java.
Adding the parenthesis fun() would call/run the function, and return 5, whereas if you don't add the parenthesis fun, it wouldn't run the function, it would just give:
<function fun at 0x000000D4B44E8730>
Whereas with the parenthesis, it would give:
5
Without calling the function (without using the parenthesis) would just give a function reference, and wouldn't run the function. Whereas if you add the parenthesis it would.
Here is an example:
def fun():
return 'The function is called!'
print(fun())
print(fun)
Output:
The function is called!
<function fun at 0x0000001615DD7730>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this function:
def a(one, two, the_argument_function):
if one in two:
return the_argument_function
my the_argument_function looks something like this:
def b(do_this, do_that):
print "hi."
Both of the above are imported to a file "main_functions.py" for my ultimate code to look like this:
print function_from_main(package1.a, argument, package2.b(do_this, do_that)
The "if one in two" from "a"function works but "b"function still executes when being passed to "function_from_main" without waiting the check from "a" to see if it actually should execute.
What can I do?
package2.b(do_this, do_that) is a function call (a function name followed by parenthesis). Instead you should be passing only the function name package2.b the function a
You will also need to modify function a such that function be is called when the condition is satisfied
# function a definition
def a(one, two, the_argument_function, argument_dict):
if one in two:
return the_argument_function(**argument_dict)
def b(do_this, do_that):
print "hi."
# function call for a
a(one, two, b, {'do_this': some_value, 'do_that': some_other_value})
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I sincerely hope this is not a duplicate, but I cannot find an answer (you'll notice that I don't even know how to ask the question!).
I have python code with one class and many def. It would take too much time to explain why I would want to do the following, but I sure would be able to:
class A:
def a(self):
some argument
def b(self):
another argument
So to call the def's, I just put:
A().a()
A().b()
What I would like to do is have a third def that looks like this:
def c(self):
process = [A().a(), A().b(), A().a(), A().a()] #This sequence is just an example - there are many more def's.
for i in process:
print 'Hello'
i
print 'Bye'
I don't know if this makes any sense? What happens currently is the process part is called and the two print lines are printed several times thereafter.
Thanks.
Might this work?
def c(self):
process = [self.a, self.b, self.a, self.a]
for i in process:
print 'Hello'
i()
print 'Bye'
In your original example, instead of storing the functions, you were calling them already when the list was built. The return values from your methods got stored in the list, so that is why they could not be called later.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I want to get incremental output each time i call the count function
import collections
result = collections.defaultdict(list)
global probability
def count():
vent ="Event"
if event in result:
probability +=1
else:
probability = 0
result[event] = {"Count":probability,"Event Type":"Login","Source":"Security","Message":"msg"}
print result[event]
count()
count()
in your count() function, the variable probability is created when the function called, it is not the same variable probability you declared at beginning.
I think you may want to use the variable as global variable.