Choose the best strategy: a simple manner and globals ()

promos = [fidelity_promo, bulk_item_promo, large_order_promo] ➊
def best_promo(order): ➋
    """选择可用的最佳折扣
    """
    return max(promo(order) for promo in promos) ➌
 
globals()
 
Return a dictionary representing the current global symbol table. This has always been the symbol table for the current module (for a function or method, it refers to the definition of their modules, instead of calling their module).
promos = [globals()[name] for name in globals() ➊
                if name.endswith('_promo') ➋
                and name != 'best_promo'] ➌
def best_promo(order):
    """选择可用的最佳折扣
    """
    return max(promo(order) for promo in promos) ➍        
❶ iteration globals () Returns the name of each dictionary.
❷ to select only the name _promo ending.
❸ filter out best_promo itself, prevent infinite recursion.
❹ best_promo internal code is not changed.
 

Guess you like

Origin www.cnblogs.com/xiangxiaolin/p/11649853.html