Dynamic method in python

I am trying to create a method in python that takes a 1-n number of parameters, calls the same method for each and returns the result. For example:

(Note that this is pseudo code, I just type these methods / syntax on the fly - new to python)

def get_pid(*params):
    pidlist = []
    for param in params:
        pidlist.add(os.getpid(param))
    return pidlist

Ideally, I would like to do something like

x, y = get_pid("process1", "process2")

Where can I add as many parameters as I want - and this method should be as "Putin" and compact as possible. I think there might be a better way than going through the parameters and adding to the list?

Any suggestions / tips?

+4
source share
2 answers

. 0 . ; list.append(); list.add().

, :

def get_pid(*params):
    return [os.getpid(param) for param in params]

; , :

x, y = (os.getpid(param) for param in ("process1", "process2"))

map() :

x, y = map(os.getpid, ("process1", "process2"))
+6

:

def get_pid(*params):
    for param in params:
        yield os.getpid(param)

x, y = get_pid("process1", "process2")
print(x, y)
+4

Source: https://habr.com/ru/post/1612053/


All Articles