Python Forum
Passing argument from top-level function to embedded function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing argument from top-level function to embedded function
#1
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)
Reply
#2
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()?
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Stopping a parent function from a nested function? wallgraffiti 1 237 May-02-2021, 12:21 PM
Last Post: Gribouillis
  Passing flags to python script, through a function xbit 4 469 Apr-20-2021, 06:32 AM
Last Post: ndc85430
Question exiting the outer function from the inner function banidjamali 3 530 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  How to use a tuple as an argument of a function zarox 5 649 Nov-14-2020, 08:02 PM
Last Post: buran
  calling a function and argument in an input phillup7 3 609 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  passing variable to function Rejoice 4 877 Sep-11-2020, 03:27 AM
Last Post: Pleiades
  Mathplotlib - passing reference to axs to function qmfoam 5 1,007 Aug-17-2020, 09:02 PM
Last Post: qmfoam
  accessing a second level nested function varsh 3 800 Aug-13-2020, 06:57 AM
Last Post: varsh
  use NULL as function parameter which is a pointer function? oyster 0 562 Jul-11-2020, 09:02 AM
Last Post: oyster
  How to use function result in another function Ayckinn 4 794 Jun-16-2020, 04:50 PM
Last Post: Ayckinn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020