I have two sorting functions that accept same (many) arguments:
def sort_A(a, b, c, d, e):
    print('sorting A', a, c, b, d, e)
    hook()
    return 'sort A result'
def sort_B(a, b, c, d, e):
    print('sorting B', e, b, a, c, d)
    return 'sort B result'
def hook():
    print('HOOK')
a = 'a'
if a == 'a':
    sorter = sort_A
else:
    sorter = sort_B
sorter(a, 'b', 'c', 'd', 'e')
I don't like the repetition in those sorting functions signatures, so I came up with two solutions:
- Make a Sorter class that would accept those many arguments in its constructor and save them for usage in methods
sort_Aandsort_B. But the code will be quite bloted with all the neededselfreferences, and I think it is not worth it for my simple use case.
- Create some kind of factory function like the one below that accepts the many arguments and by doing so provides context for sorter functions defined in it.
But is solution 2 a good practice? Specifically, is it OK for a factory function like this to have some other function definitions in its scope? It seems like breaking single responsibility principle - the factory function not only returns the object, but also defines it. It is also probably not best performance-wise, since the definitions are interpreted at each call of the factory function.
def get_sorter(a, b, c, d, e):
    def sort_A():
        print('sorting A', a, c, b, d, e)
        hook()
        return 'sort A result'
    def sort_B():
        print('sorting B', e, b, a, c, d)
        return 'sort B result'
    def hook():
        print('HOOK')
    if a == 'a':
        return sort_A
    return sort_B
sorter = get_sorter('a', 'b', 'c', 'd', 'e')
print(sorter())
 
    