Ordered Member Functions of a Class in Python

# References:
# 1. https://stackoverflow.com/questions/48145317/can-i-add-attributes-to-class-methods-in-python

from functools import wraps

# Define a decorator
def attributes(**attrs):
    """
    Set attributes of member functions in a class.

    ```
    class AGoodClass:
        def __init__(self):
            self.size = 0

        @attributes(order=1)
        def first_good_member(self, new):
            return "first good member"

        @attributes(order=2)
        def second_good_member(self, new):
            return "second good member"
    ```

    References:
    1. https://stackoverflow.com/a/48146924/1477359
    """
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            return f(*args, **kwargs)

        for attr_name, attr_value in attrs.items():
            setattr(wrapper, attr_name, attr_value)

        return wrapper

    return decorator


class AGoodClass:
    def __init__(self):
        self.size = 0

    @attributes(order=1)
    def first_good_member(self, new):
        return "first good member"

    @attributes(order=2)
    def second_good_member(self, new):
        return "second good member"


# Test

agc = AGoodClass()

print(agc.first_good_member.order)
print(agc.second_good_member.order)

Planted: by ;

LM (2020). 'Ordered Member Functions of a Class in Python', Datumorphism, 12 April. Available at: https://datumorphism.leima.is/til/programming/python/python-class-methods-ordered/.