Python Forum
Decorator vs. evaluate function in function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decorator vs. evaluate function in function
#1
Hi there,

in my project I use assertation functions similar to the one below to perform, for example, bounds checking:

def assert_greater_zero(x):
    if x < 0.0:
        raise ValueError('x out of bounds.')
Typically, I would use such functions to check the arguments of some __init__ function. Additionally, my interface allows for None, which should be passed through to the instance variable. Of course, the assert would fail if I passed it None, hence I have to check:

class House:
    def __init__(self, n_windows)
        if n_windows is not None:
           assert_greater_zero(x)
        self.n_windows = n_windows
Since there are lots of such checks in my project, I started to look for a way to hide them. I came up with the following two ideas:

1. A decorator
def pass_through_1_(func):
    def wrapper(arg):
        if arg is not None:
            func(arg)
        return arg
    return wrapper
Then I thought "why do I even need a decorator? I could simply evaluate the assert". So, the second approch uses only the wrapper part of the decorator.

2. A function that evaluates the assert function
def pass_through_2_(func, arg):
    if arg is not None:
        func(arg)
    return arg
So in the class, the frist approch would look like:
class House:
    def __init__(self, n_windows)
        self.n_windows = pass_through_v1_(assert_greater_zero)(n_windows)
and the second approch would be

class House:
    def __init__(self, n_windows)
        self.n_windows = pass_through_v2_(assert_greater_zero, n_windows)
This makes up three different ways to solve the same problem. The explicit one, which performs the check in __init__, the decorator version and the third one, which evaluates the assert. What I want to discuss is which way would you prefer? Personally and until now, I like the third approch the most, because it keeps my __init__ clean and I can avoid the "decorator(func)(arg)" syntax. On the other hand, it is not clear from the call that "pass_throuh" also handles the None case.

What do you think?
Reply
#2
Why not this
def checked_none_or_greater_zero(x):
    if not (x is None or x >= 0):
        raise ValueError('Expected None value or greater than zero, got', x)
    return x

...
        self.n_windows = checked_none_or_greater_zero(n_windows)
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 799 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  the order of running code in a decorator function akbarza 2 572 Nov-10-2023, 08:09 AM
Last Post: akbarza
  Curious about decorator syntax rjdegraff42 14 2,249 May-03-2023, 01:21 PM
Last Post: rjdegraff42
  Exit function from nested function based on user input Turtle 5 2,990 Oct-10-2021, 12:55 AM
Last Post: Turtle
  ABC Module and @property decorator, Pythonic Way? muzikman 21 5,877 Aug-18-2021, 06:08 PM
Last Post: muzikman
Question Stopping a parent function from a nested function? wallgraffiti 1 3,717 May-02-2021, 12:21 PM
Last Post: Gribouillis
Question exiting the outer function from the inner function banidjamali 3 3,621 Feb-27-2021, 09:47 AM
Last Post: banidjamali
  Passing argument from top-level function to embedded function JaneTan 2 2,301 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  decorator adamfairhall 0 1,586 Aug-18-2020, 08:38 AM
Last Post: adamfairhall
  use NULL as function parameter which is a pointer function? oyster 0 2,531 Jul-11-2020, 09:02 AM
Last Post: oyster

Forum Jump:

User Panel Messages

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