Python Forum
Decorator inhibits execution of function if non-None parameter not supplied
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorator inhibits execution of function if non-None parameter not supplied
#1
If a function is supposed to receive a parameter other than None, but for whatever reason the value of the parameter is None, this decorator will inhibit the function from executing. Very useful when required command line arguments are missing. Don't forget to import sys!

Here's the decorator:

def none_decorator(func):
    def none_deco(*args):
        if len(sys.argv) > 1:
            return func(*args)
    return none_deco
And a potential application:

@none_decorator
def some_function(some_non_none_parameter):
    #put some code here
Reply


Messages In This Thread
Decorator inhibits execution of function if non-None parameter not supplied - by league55 - Feb-25-2018, 03:30 PM

Forum Jump:

User Panel Messages

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