I have defined a function taking a variable number of arguments.
These arguments will all be lists of floats, each variable having a name, say a
, b
, c
, for 3 arguments. They will look like:
a=[1,2]
b=[3,4]
c=[5,6]
I want to be able to work with the name of these arguments, ie
def function(*args):
for arg in args:
plt.plot(x_value, arg, label="the name of arg")
I call the function as function(a,b,c)
"the name of arg"
shall be the string a
for the first iteration of the for loop, the string b
for the second iteration of the for loop and the string c
for the third iteration of the for loop.
What shall I put there at label=
? I have tried various approaches, they all result either in printing the string arg
, or the actual value of arg
, ie [1,2] for the first iteration of the for-loop.
Thank you!
If you want the argument names, you have to use named arguments or a dictionary.
def function(**kwargs):
for name, value in kwargs.items():
plt.plot(x_value, value, label=name)
function(a=a, b=b, c=c)
This passes three named arguments to the function, a
, b
, and c
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.