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
  The function of double underscore back and front in a class function name? Pedroski55 9 560 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  mutable argument in function definition akbarza 1 423 Dec-15-2023, 02:00 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,026 Dec-25-2022, 03:00 PM
Last Post: askfriends
  passing dictionary to the function mark588 2 931 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  i want to use type= as a function/method keyword argument Skaperen 9 1,771 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Taking any 2d image and passing it to create a level iki_a_toure 4 984 Oct-19-2022, 06:35 AM
Last Post: iki_a_toure
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle
  Passing Argument Errors cosmarchy 6 2,376 Sep-29-2021, 06:09 AM
Last Post: snippsat
  Regex - Pass Flags as a function argument? muzikman 6 3,482 Sep-06-2021, 03:43 PM
Last Post: muzikman
Question Stopping a parent function from a nested function? wallgraffiti 1 3,613 May-02-2021, 12:21 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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