0

I came across a python program question. Below is the question and its corresponding solution. I just could not understand the flow of the program. Especially the @person_lister, and its significance.

Also, print(*name_format(people), sep='\n') name_format here is taking 2D array instead of 1D. As per the definition of name_format it possibly expects a 1D array.

Can someone please explain me the flow of this program I just cannot understand it.

Appreciate your help. Thanks.

Question https://www.hackerrank.com/challenges/decorators-2-name-directory/problem

Solution:

import operator

people = [input().split() for i in range(int(input()))]

def person_lister(f):
    def inner(people):
        return [f(person) for person in sorted(people, key=operator.itemgetter(2))]
    return inner

@person_lister
def name_format(person):
    return ("Mr. " if person[3] == "M" else "Ms. ") + person[0] + " " + person[1]

print(*name_format(people), sep='\n')
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Does this answer your question? [How to make a chain of function decorators?](https://stackoverflow.com/questions/739654/how-to-make-a-chain-of-function-decorators) – Green Cloak Guy Apr 24 '20 at 19:39
  • The short answer is that `person_lister()` changes `name_format()` into `inner()` - calling `name_format()` on each of the sublists in `person`, all in turn. – Green Cloak Guy Apr 24 '20 at 19:40

0 Answers0