back

by yamrzou·6y ago·view on hn ↗
In Python you can use class-like functions (source : https://charemza.name/blog/posts/python/state/you-might-not-...)

    def MyClass(...)  # Capitalised to make it clear it's a "constructor"

        state_1 = ...
        state_2 = ...

        def func_1():
            nonlocal state_1
            ...

        def func_2():
            nonlocal state_2
            ...

        def func_3():
            ...

        return func_1, func_2  # Typically, 1 to 2 funcs
I find the approach neat. Here is a concrete example where it is applied : https://github.com/uktrade/mobius3/blob/master/mobius3.py