Python Forum
Passing argument from top-level function to embedded function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Passing argument from top-level function to embedded function (/thread-30306.html)



Passing argument from top-level function to embedded function - JaneTan - Oct-15-2020

Hi,
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_b=excel_sht.cell(row=i, column=excel_col).value='OK'

eval_value(sht, 9, mh_criticalval)
Check('test.xlsm',1)



RE: Passing argument from top-level function to embedded function - bowlofred - Oct-15-2020

What do you mean by "function within a function"?  The code above has two different functions, and neither is within the other.  (I'm assuming the bad indentation on the second line is just a typo).  You say "doesn't work", but you don't describe what is happening and how that differs from what you expect to happen.

Check() doesn't do anything because it doesn't modify any data outside the function, and it doesn't return any data.  The way it's written, Check() is useless.

What do you want Check() to do, and why would it have any effect on eval_value()?


RE: Passing argument from top-level function to embedded function - deanhystad - Oct-15-2020

So what is the code supposed to be?

In this example eval_value() is hidden inside Check()
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_b=excel_sht.cell(row=i, column=excel_col).value='OK'
 
    eval_value(sht, 9, mh_criticalval)

Check('test.xlsm',1)
In this example Check() and eval_value() are both visible to the module and Check() uses eval_value().
def Check(file_name, mh_criticalval):
    test_file=file_name
    eval_value(sht, 9, mh_criticalval)
 
def eval_value(excel_sht, excel_col, mh_value):
   if excel_sht.cell(row=i, column=excel_col)>mh_value:
        cell_b=excel_sht.cell(row=i, column=excel_col).value='OK'
 
Check('test.xlsm',1)
In this example Check() and eval_value() are both visible to the module and are called separately.
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_b=excel_sht.cell(row=i, column=excel_col).value='OK'
 
eval_value(sht, 9, mh_criticalval)
Check('test.xlsm',1)
I'm pretty sure this last one is not what you want as it makes no sense. But none of the examples make much sense to me because I see no reason why file_name is passed to Check() and then isn't used for anything. What is the purpose of Check()? Did you cut out some code that opens a file and reads in a spreadsheet? If that is the case you will want to use the first example.