Python Forum
In this function y initially has no value, but a call to foo() gives no error. Why?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In this function y initially has no value, but a call to foo() gives no error. Why?
#7
Remember that calling a function and defining a function are two completely different things. The latter creates a function that can later be called and declares what parameters must be given at call time. Since there is no call to the function then, there's no checking of what parameters are declared (how could there be?). That is the same regardless of whether that definition is nested inside another function or not. Calling a function necessitates passing the right number of arguments and obviously checking against the definition only happens at call time. This happens regardless of what scope that call is in (whether inside a function or at a global level, for example).

Let's say you want to measure execution times of function calls and record those somewhere. Of course, you could just change all your functions to do that, but you'd be repeating similar code everywhere. Another approach is to write a function that takes as its argument a function f and produces a new one that calls f and does the measurement and recording of the time:

def measure_time(f):
  def measured():
     start_time = calculate_time()
     result = f()
     end_time = calculate_time()
     record_time(end_time - start_time)
     return result
  return measured
(I've used fictional functions and just taken the case that f takes no arguments, for the sake of simplicity).

This is known as a decorator and Python has syntax to decorate functions:

@measure_time
def do_something_useful():
  ...
Here, basically, with the presence of the @measure_time decorator, do_something_useful will really be the function that measure_time returns (i.e. one that does exactly the same as do_something_useful but with the additional recording of the execution time.
Pedroski55 likes this post
Reply


Messages In This Thread
RE: In this function y initially has no value, but a call to foo() gives no error. Why? - by ndc85430 - Dec-17-2020, 06:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 841 May-02-2023, 08:40 AM
Last Post: Gribouillis
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 error from Mysql call AkaAndrew123 1 3,505 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  how to call an object in another function in Maya bstout 0 2,111 Apr-05-2021, 07:12 PM
Last Post: bstout
  Struggling for the past hour to define function and call it back godlyredwall 2 2,264 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,968 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  function call at defined system time? Holon 5 3,282 Oct-06-2020, 03:58 PM
Last Post: snippsat
  How to call/read function for all elements in my list in python johnny_sav1992 1 2,121 Jul-27-2020, 04:19 PM
Last Post: buran
  Python: Call function with variabele? Ending in error. efclem 5 2,998 Apr-22-2020, 02:35 PM
Last Post: buran
  How to mock an object that is created during function call? Schlangenversteher 0 1,998 Jan-31-2020, 01:36 PM
Last Post: Schlangenversteher
  Is there a way to search for function call? mtran 2 2,305 Jan-14-2020, 02:07 AM
Last Post: mtran

Forum Jump:

User Panel Messages

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