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>
Related
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 1 year ago.
Improve this question
I tried searching online but did not find any answer to this particular question. In python there are no function declarations and a function can't be used until it has been defined.
Does this mean that indirect recursion is impossible in python?
And is there a way around this by using some modules?
No, it is possible
def f():
print('from f')
g()
def g():
print('from g')
f()
"a function can't be used until it has been defined" is not so straightforward. When the code runs, the name of the objects that it refers to have to exist. So, you can't do
f()
def f():...
because f() actually executes something. But definitions create a function object, without running it at the time. In the example, the function is claled at the last line of the script, and, by that time, both f, g do exist.
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 1 year ago.
Improve this question
I'm a novice python programmer and thinking this is a very simple task.
I'm trying to use a function argument as a value within a df variable when calling the function, but it is returning the argument address and not the argument value.
def func_name(var_name):
df['varname']=str(var_name)
func_name(split_rand)
df
I want varname to contain "split_rand" throughout, but it contains <function split_rand at 0x0000025E4EAD9A60>. I know that enclosing 'split_rand' in quotes will work, but I don't want to use that for alternative reasons.
Thank you
It's returning the string representation of that function name. If you want the actual function name, then you do func.__name__
I think you meant to do df['varname']=var_name.__name__ instead and I'd rename var_name to func.
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 7 years ago.
Improve this question
Admission: in python, I sometimes forget to include the () when I'm calling a method, so I accidentally use object.method instead of object.method().
The error I get often appears to indicate that my object.method call has returned the method itself, instead of calling the method. I'm wondering if this means that by using object.method or perhaps just function you can pass functions/methods around as arguments?
Bonus: If so, what are some applications of this? I know, for example, that lambdas can be used to pass anonymous functions as parameters.
It returns a function object that you could use later. For example
def myPrint(fun):
for i in range(5):
print(fun(i))
def myFactorial(x):
acc = 1
for i in range(1,x):
acc *= i
return acc
Then you can do
>>> myPrint(myFactorial)
1
1
2
6
24
Or
>>> myPrint(range)
[]
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
This allows you to create a function that you want to use in other context (And it's pretty common in python), lot of built-in functions do this, like map, filter, reduce, sort...
As you can see myObject.myFunction returns the function itself while myObject.myFunction() actually "executes" it
It is used for so called callbacks. YOu can pass the function as argument and after you have done your magic in the function call the callback. Usefull if you do async stuff.
Yes. object.method returns the method as an object.
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)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
def f():
return True
print(f())
or
def f():
return True
if f():
print('True')
else:
print('False')
Should I round it in if statement or simply print out the value, in Python 3.3.2?
print automatically converts input to its string representation. So, the two methods actually do the exact same thing. Considering that, why not use the first? It is a lot cleaner.
Also, the second method can really be simplified:
print('True' if f() else 'False')
Having that much code for something so simple isn't necessary.
If your function always returns a bool, then just print it, like this:
print(f())
If you need to convert it to a string without printing it, use str(f()) or repr(f()). For the bool type these two are equivalent, but e.g. if their argument is an str, they are different: repr(str_obj) returns it properly quoted and escaped.
If it's not always a bool, but you want to print something depending on its truth value, then use an if, like in your question or in iCodez' answer. Alternatively, you can convert it to a bool, and then print it:
print(bool(f()))
Personally, the:
print(f())
form looks nicer. However, one thing to consider is that you have baked in a call to print what is essentially debugging output with the call that is causing the functionality to be invoked. This would make it painful later when you want to turn off all the annoying print statements when you are ready to build upon your program.
You could instead have a function such as:
LOGGING_ENABLED = True
def print_if(my_bool):
if LOGGING_ENABLED:
print str(my_bool)
This will ensure that the function is always invoked but the debug output happens only if you want it.