0

I'm very new to Python. I'm looking to return a varying number of objects (will eventually be lists or pandas), ideally with their original name.

So far I'm looking at something like this:

def function(*Args):
    Args = list(Args)    
    for i in range(len(Args)):
        Args[i] += 1
        print Args[i]
    return *all variables with original name, in this case a, b, c, d *

a=1
b=2
c=3
d=4

a, b, c, d = function(a, b, c, d)

Any help, ideas or comments would be very appreciated. Thanks.

reformed
  • 4,505
  • 11
  • 62
  • 88
Tobias
  • 59
  • 5
  • 1
    Possible duplicate of [How do I create a variable number of variables in Python?](http://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables-in-python) – Morgan Thrapp Sep 07 '16 at 17:00
  • 2
    There isn't really a way to do this -- Nor should you really want to. For example, the value `1` could have _many_ names in the calling namespace: `a = aa = aaa = 1`. Depending on what you want to do, you _might_ be able to make some use of the `locals` or `globals` builtins, but generally you probably shouldn't need that. What's the endgame here? Why do you want this? – mgilson Sep 07 '16 at 17:01
  • If your function were to be called as `function(float("NaN"), a if b else c, d+e, 4+5)`, what are the "original" names of its arguments? – kindall Sep 07 '16 at 17:04
  • Eventually it is for an external data request, so in a, b, c, etc I would specify what I want to request (the will be lists with multiple items) and depending on how many arguments i pass over, I also want to given back the same number of results. Is it possible to generally return a dynamic number of object? – Tobias Sep 07 '16 at 17:11
  • @Tobias why not returning the objects in a list or in a dict ? – Xavier C. Sep 07 '16 at 17:35
  • @mgilson yes you're right, i should have stated my problem more clearly. eventually it's a datarequest from bloomberg. The passed arguments are lists (lets say of different stock tickers), and the list are for different industry groups. the one time i might request stock data for energy, industrial and sutomotive companies, but the other time i might request for consumer and health care companies. the lists i would name after the industry and would therefore like to preseve. Now i did it with a list as Xavier C. suggested. but unfortunately then the industry group name gets lost. – Tobias Sep 08 '16 at 09:54

2 Answers2

0

Are you just looking to return the tuple?

def function(*Args):
    Args = list(Args)    
    for i in range(len(Args)):
        Args[i] += 1
        print Args[i]
    return Args

 a, b, c, d = function(a, b, c, d)
Eric
  • 95,302
  • 53
  • 242
  • 374
  • I agree this does what the OP is asking for in the specific example, but I'm wondering if they in fact want something equivalent to `*Args = function(*Args)`? – dblclik Sep 07 '16 at 17:32
  • This function can be used as `Args = function(*Args)`? I'm not sure what exactly is being asked for here, but I can't really see what else it could be – Eric Sep 07 '16 at 18:27
  • I completely agree--I'm wondering if the OP shouldn't just return a `dict` with a Dummy ID as the key and the name and value as a tuple so that he always has just one return value. It's not well-formed as it is asked – dblclik Sep 07 '16 at 19:15
  • Yes, as a dict or list that works, and serves the main purpose, the only thing is the there is information in the variable name i pass over which i wouldnt like to lose. Eventually its fine and i can work around it by specifying this information again after running the function. I just hoped that there is a way to preserve the information to make to process a bit more automated – Tobias Sep 08 '16 at 10:08
0

In the meantime I tried it as @Xavier C. suggested and it seems to work. the lists/input arguments i would name after the industry and would therefore like to preseve. Is there any way to achieve that?

def BBGLiveRequest(*args):
    data = []

    for i in range(len(args)):    
        print args[i]
        result = BLPTS(args[i], ['PX_Last'])    
        result.get()
        print result.output
        data.append(result.output)

    return data
Tobias
  • 59
  • 5
  • Use a dictionary by replacing `data = []` with `data = {}` and `data.append(result.output)` with `data[args[i]] = result.output`. You can then access each result by `data['oneofinputarguments']`. By the way, if you set the loop to `for i in *args` the variable `i` is automatically equal to `args[i]` in your case. It's one of the nicer Python features and saves a lot of typing. – StefanS Sep 08 '16 at 10:17