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 have a file (test.py):
def main():
def hello():
x = 10
Then I have another file (test2.py):
from test import *
print(main.hello.x)
Now I know that print(main.hello.x) won't work but I want something like that.
I want to access a variable inside another file that is in a nested function.
How can I do that?
This feels quite unpythonic, but this will work:
def main():
def hello():
hello.x = 10
return hello
main.hello = hello
return main
print(main.hello.x) #returns 10
a more pythonic approach is to use a class.
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
Can I simply this function:
def fun1(param, value, send):
if send == True:
fun2(**{param:value})
else:
fun2()
You could put the if...else inside the function argument:
def fun1(param, value, send):
return fun2(**({param:value} if send else {}))
Another way to write this function is as follows:
def fun1(param, value, send):
fun2(**{param:value}) if send == True else fun2()
The syntax is:
a if condition_meets else b
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 4 years ago.
Improve this question
In the following Python code, what does the (x) mean?
Dropout(0.8)(x)
(x) uses the preceding function reference (returned by Dropout(0.8)) to make a function call with x as the argument.
For example:
def Dropout(a):
def func(b):
return a + b
return func
x = 0.2
print(Dropout(0.8)(x))
would output 1.0.
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 7 years ago.
Improve this question
Can anyone explain the reason why python behaves differently in the following two cases please? Much appreciation.
def modifyNone(x):
print("B4:"+str(x))
# x.append(5)
x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[]
Method:
def modifyNone(x):
print("B4:"+str(x))
x.append(5)
# x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[5]
Python is pass by value, so you'll have to reassign a returned value like so:
a = modifyNone(a) # where the function returns a value
You don't seem to understand variable scope as well, try the documentation.