0
  1. How does args work? I know it is used when you don't know how many arguments you may pass to a function, but I need deeper explanation. Do they run function for each argument passed?
    def buy_sell_hold(*args):
        cols = [c for c in args]
        requirement = 0.02
        for col in cols:
            if col > requirement:
                return 1
            if col < -requirement:
                return -1
        return 0
    def extract_featuresets(ticker):
        tickers, df = process_data_for_labels(ticker)

        df['{}_target'.format(ticker)] = list(map( buy_sell_hold,
                                               df['{}_1d'.format(ticker)],
                                               df['{}_2d'.format(ticker)],
                                               df['{}_3d'.format(ticker)],
                                               df['{}_4d'.format(ticker)],
                                               df['{}_5d'.format(ticker)],
                                               df['{}_6d'.format(ticker)],
                                               df['{}_7d'.format(ticker)] ))
    vals = df['{}_target'.format(ticker)].values.tolist()
    return vals

  1. Vals are single list how so, I'm lost.
therealak12
  • 1,178
  • 2
  • 12
  • 26
gg.khomar
  • 1
  • 2
  • 2
    These questions have been asked multiple times, it is advisable to search the already available knowledge base of SO, or Google for that matter, before asking a question. See [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/5431791) e.g. answer on [`map`](https://stackoverflow.com/q/10973766/5431791) – Sayandip Dutta Jan 17 '21 at 18:44

1 Answers1

0

python has two types of variable length arguments:

  • *args (Non Keyword Arguments)
  • **kwargs (Keyword Arguments)

When you define a function with *args, you effectively take arguments and collect them into a list. So;

def foo(*a):
    # a here is a tuple: (2,3,4,5)
    print(a)

foo(2,3,4,5)
>>>
(2,3,4,5)

When you define a function with **kwargs, you effectively take named arguments and collect them into a dict. So;

def zoo(**b):
    # b here is a dict: {'test': 2, 'abc': 'def'}
    print(b)
zoo(test=2, abc='def')
>>>
{'test': 2, 'abc': 'def'}

When you want to pass variable arguments from a list or a dict, you can use similarly:

def xxx(a=2, b=4):
    print(a+b)
kw = {'a': 5, 'b': 7}
xxx(**kw)
>>>
12

see: https://www.programiz.com/python-programming/args-and-kwargs#:~:text=Python%20has%20*args%20which%20allow,to%20pass%20variable%20length%20arguments.

Dharman
  • 30,962
  • 25
  • 85
  • 135
armamut
  • 1,087
  • 6
  • 14