Python Forum
i need a module for more involved command line parsing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need a module for more involved command line parsing
#9
To understand a decorator, you have to understand what a closure is.

def function(a):
    b = 40
    def inner_function():
        print(a,b)
    inner_function()  # <-- calls the inner_function
    # inner function has access to the scope of function
If you rewrite the inner_function as anonymous lambda function, it's more clear:
def function(a):
    b = 40
    inner_function = lambda: print(a, b)
    inner_function()  # <-- calls the inner_function
    # inner function has access to the scope of function
Decorators should take a function and modify the behavior (input_values, return_values).
This can be reached with a closure, which returns the inner function.

def square(function):
    def inner(*args, **kwargs):
        return function(*args, **kwargs) ** 2
    return inner # <- returns the inner function without calling it


def foo():
    return 42


print(foo())
# the direct call together with the decorator looks like this
square(foo)()  # <- square(foo) returns inner, inner is called
# assign the decorated function to a name
square_foo = square(foo) # square_foo is now the function inner
# calling square_foo
square_foo()
To @ sign for the decorator is just a syntactical sugar for us.

@wrapper
def func():
    return

# is the same like
def func():
    return
func = wrapper(func)
# old name is overwritten by inner function
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: i need a module for more involved command line parsing - by DeaD_EyE - Aug-27-2019, 08:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  module either imported by a command or itself run as a command Skaperen 2 642 Dec-04-2023, 03:15 AM
Last Post: Skaperen
  review of command line parsers Skaperen 2 2,099 Mar-11-2021, 07:39 PM
Last Post: Skaperen
  command line options Skaperen 5 2,712 Aug-14-2020, 08:48 AM
Last Post: DeaD_EyE
  opening python from the command line takes a long time to load? abdulkaderanwar 4 3,059 Jun-22-2020, 03:42 AM
Last Post: abdulkaderanwar
  f-string in command line arguments Skaperen 0 1,623 May-05-2020, 11:49 PM
Last Post: Skaperen
  my own command line option parser Skaperen 0 1,682 Mar-27-2020, 04:14 AM
Last Post: Skaperen
  want suggested module for e-mail parsing Skaperen 0 1,562 Jul-26-2019, 08:52 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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