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 2 years ago.
Improve this question
This is a simplified example. But basically, I have function within a function. I want to pass the argument from the top-level function to the embedded function, but it doesn't work. How do I solve this?
Thank you
def Check(file_name, mh_criticalval):
test_file=file_name
def eval_value(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col) > mh_value:
cell_cny_b=excel_sht.cell(
row=i,
column=excel_col
).value='OK'
eval_value(sht, 9, mh_criticalval)
Check('test.xlsm',1)
Method 1: De-Nesting
The most obvious method here is to remove the nested functions. Doing this is simple:
def eval_mh(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col)>mh_value:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
eval_mh(sht, 9, mh_criticalval)
CheckDiag('test.xlsm',1)
All we did here was move the inner function out. However, if you still wanted them to be nested, try the following.
Method 2: Local/Global Variable Exploitation
Here, we replace all instances of mh_value with mh_criticalval
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
def eval_mh(excel_sht, excel_col):
if excel_sht.cell(row=i, column=excel_col)>mh_criticalval:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)
Method 3: Why 2 functions?
In this method, we remove the function entirely!
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
if sht.cell(row=i, column=9)>mh_criticalval:
cell_cny_b=sht.cell(row=i, column=9).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)
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 yesterday.
Improve this question
I have code:
def users_list_reaction(users_dict, prefix_dictionary, event):
login = event.widget.get()
prefix_str = users_dict.get(f'{login}')
for i in prefix_str[0]:
if i == ' ':
pass
else:
for x in i:
prefix_dictionary[x].select()
But I only need to pass two of the three arguments. I have error TypeError: users_list_reaction() missing 1 required keyword-only argument: 'event'. None how answer will not help, because the event is a tkinter library widget and it is needed to get information from the combobox. I don't want use global users_dict . What to do?
I tried to search for information on the Internet, but I don’t understand how to formulate it correctly, because I didn’t find an answer to mine
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
i wanna create a Reverse word function, and i tried:
def reversa(str):
j=len(str)
for i in str:
j -=1
print(str[j], end="")
print(reversa("Apple"))
But it did not work.
Well there are existing functions for this already:
print("Apple"[::-1])
And:
print(''.join(reversed("Apple")))
Your code doesn't work because:
since there's no return and only print, just call like: reversa("Apple")
Also, would be nice to print() at the end, like (fixed up some stuff):
def reversa(s):
j=len(s)
for i in s:
j-=1
print(str[j], end="")
print()
print(reversa("Apple"))
Also if i where you, i would make a function like:
def reversa(s):
return ''.join([s[-i] for i in range(1,len(s)+1)])
Which could be called like:
print(reversa('Apple'))
You may want to use range to increment backwards from the largest index position
def reversal(str):
for i in range(len(str) - 1, -1, -1):
print(str[i], end="")
reversal("Apple")
The loop is not running one more time.
The thing is you are printing the function reversa (i.e. the return value of the function). Since you don't make the function return anything, None will be returned and printed after your function has ended.
You just need to call the function without print: reversa("Apple")
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
I am trying to convert a lambda function in normal function to use it as a key instead but i couldn't understand it.
def most_appear(name_list):
return max(name_list, key=lambda x: name_list.count(x))
How this lambda work?
How I can create a function to use as a key?
It's just slightly more convenient syntax for:
def func(x):
return name_list.count(x)
and then
... max(..., key=func)
This function counts the amount for each element and returns the element with most elements. The lambda is not necessary, since name_list.count already is a function:
def most_appear(name_list):
return max(name_list, key=name_list.count)
in this case, if you have two arguments for function, you could use partial function
from functools import partial
def replace_lambda(lst, item):
return lst.count(item)
def most_appear(name_list):
func = partial(replace_lambda, name_list)
return max(name_list,key=func )
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.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
here I want to call web service function only once throughout the program.
how to accomplish this anybody suggest me
import sys,os
def web_service(macid):
# do something
if "__name__" = "__main__" :
web_service(macid)
This is how I would to that:
i_run_once_has_been_run = False
def i_run_once(macid):
global i_run_once_has_been_run
if i_run_once_has_been_run:
return
# do something
i_run_once_has_been_run = True
#Vaulstein's decorator function would work too, and may even be a bit more pythonic - but it seems like a bit overkill to me.
Using class,
class CallOnce(object):
called = False
def web_service(cls, macid):
if cls.called:
print "already called"
return
else:
# do stuff
print "called once"
cls.called = True
return
macid = "123"
call_once_object = CallOnce()
call_once_object.web_service(macid)
call_once_object.web_service(macid)
call_once_object.web_service(macid)
Result is,
I have no name!#sla-334:~/stack_o$ python once.py
called once
already called
already called
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
The pseudocode is like this:
def a_func(parent_node, child_node):
parent_node.add(child_node)
// check validity
return child_node
node1 = a_func(pnode, Node(attr_x = "a new node")
node2 = a_func(pnode, Node(node1.get_attr("attr_x"))
It's used in tree structure. a_func insert nodeX into a parent nodeY and return nodeX. Does anyone have ideas about a suitable name for it?
I would name the function something like enlistForParent or maybe addToParent or even setParentForNode I believe all these versions imply that the return value is the child note and also make the effect of calling the function clear.