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.
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'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 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 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 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
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 8 months ago.
Improve this question
I have two files File1 and File2. In File1 I have a function detect() defined as:
def detect():
s = // some operations
I need to access this variable s in File2.
I have tried declaring the variable as global but it has not worked.
How can I access the variable without creating a class as in this post or by using __main__ as in this post ??
function detect must be run to init its local variables.
def detect():
detect.tmp = 1
def b():
print(detect.tmp)
detect()
b()
of course you can import one python file as python module and call its functions like
from File1 import detect
print(detect.tmp)
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 6 years ago.
Improve this question
Suppose I have a function -
def foo(x,y):
pass
a = foo(5,6)
How do I access the values 5 and 6 from a?
From the code you have shown us, you cannot -- 5 and 6 were passed in to foo, you didn't keep a copy of them, foo didn't keep a copy of them, so they are gone.
So, as the above paragraph hinted, somebody has to keep a copy of those arguments if you want to do something else with them later, and while it is possible to have a function do so, that's not really what they are intended for. So your easy options are:
make foo a class that saves the arguments it was called with (which is still highly unusual), or
save the arguments yourself (arg1, arg2 = 5, 6 for example)
You can't. You'd need to use an object.