Python Forum
The of ( : ) what does it do?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The of ( : ) what does it do?
#10
You are wrong if you think type hints has anything to do with why function_log prints (not returns) "La méthode __init__ de la classe <class 'nutrient.Nutrient'> est appelé". Python knows how to get this information without the aid of type hints.

You are correct that *args is a tuple and **kwargs is a dictionary, but they have nothing to do with prepare_takeoff_and_landing(function) returning a function.

In both cases what you are seeing is a decorator, a function that adds some extra processing to existing functions.

function_log(function) prints information about the function call before calling the function.

prepare_takeoff_and_landing(function) calls get_off_ship() and take_off_ship() before calling the function, then it calls land_ship(), exit_the_ship() and open_the_hold().

What is neat about decorators is they led you add this extra processing to your functions without you having to do any programming other than declaring that your function uses the decorator. For example, if you had a function named "do_interesting_stuff()" you could have it do logging by simply "decorating" the function definition
@function_log
def do_intersting_stuff(a, b, c, d):
    """This function does interesting stuff"""
    ...
Now when you program calls "some_value = do_interesting_stuff(a, b, c, d)" Python actually does this:
self = args[0]
print(f"La méthode {function.__name__} de la classe {self.__class__} est appelé")
some_value = do_interesting_stuff(a, b, c, d)
I think this is a nice writeup about decorators.

https://www.geeksforgeeks.org/decorators-in-python/
ndc85430 and Frankduc like this post
Reply


Messages In This Thread
The of ( : ) what does it do? - by Frankduc - Jul-30-2022, 02:44 PM
RE: The of ( : ) what does it do? - by deanhystad - Jul-30-2022, 05:11 PM
RE: The of ( : ) what does it do? - by Frankduc - Jul-30-2022, 05:48 PM
RE: The of ( : ) what does it do? - by deanhystad - Jul-30-2022, 09:06 PM
RE: The of ( : ) what does it do? - by mHosseinDS86 - Jul-31-2022, 09:33 AM
RE: The of ( : ) what does it do? - by ndc85430 - Jul-31-2022, 10:16 AM
RE: The of ( : ) what does it do? - by Frankduc - Jul-31-2022, 11:48 AM
RE: The of ( : ) what does it do? - by DeaD_EyE - Jul-31-2022, 03:31 PM
RE: The of ( : ) what does it do? - by Frankduc - Jul-31-2022, 03:57 PM
RE: The of ( : ) what does it do? - by deanhystad - Jul-31-2022, 05:22 PM
RE: The of ( : ) what does it do? - by Frankduc - Jul-31-2022, 06:35 PM
RE: The of ( : ) what does it do? - by deanhystad - Aug-01-2022, 03:50 AM

Forum Jump:

User Panel Messages

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