Is it possible to alternate the use of *args and **kwargs?
Imagine I have a function that looks like this :
myFunction(arg, arg, kwarg, arg, arg, kwarg, etc...):
Where arg is an *arg and kwarg is a *kwarg. Before now, my function looked like myFunction(*args):
and I was using just a long list of *args and I would just pass in a big list like this
myFunction(*bigList):
The bigList looked like = [[1,2,3],[4,5,6],'hello',[1,3,5],[2,4,6],'world',etc...]
But, now I need to have a kwarg every third argument. So, in my mind, the list "looks" like this now:
newBigList = [[1,2,3],[4,5,6],word='hello',[1,3,5],[2,4,6],word='world',etc...]
So, there are two questions to make this work.
1) Can I construct a list with a string for a kwarg without the function reading it in as an actual argument? Could the word
(s) in the newBigList
be strings?
2) Can you alternate kwargs and args? I know that kwargs are usually done with dictionaries. Is it even possible to use both by alternating?
As always, if anyone knows a better way of doing this, I would be happy to change the way I'm going about it.
EDIT Here's the method. Its a matplotlib method that plots a polygon (or a bunch of polygons):
plot([x1], [y1], color=(RBG tuple), [x2], [y2], color=(RGB tuple), etc...)
Where [x1]
is a list of x values for the first polygon, [y1]
is a list of y values for the first polygon, and so on.
The problem is, to use RBG values for the color argument, I need to include the color keyword. To further complicate matters, I am generating a random tuple using the random.random()
module.
So, I have this list of lists of x values for all the polygons, a list of lists of y values for all my polygons, and a list of tuples of random RBG colors. They look something like this:
x = [[1,2,3], [4,5,6], [7,8,9]]
y = [[0,9,8], [7,6,5], [4,3,2]]
colors = [(.45, .645, .875), (.456, .651, .194), (.813, .712, .989)]
So, there are three polygons to plot. What I had been doing before I could do keywords was zip
them all up into one tuple and use it like this.
list_of_tuples = zip(x, y, colors)
denormalized = [x for tup in list_of_tuples for x in tup]
plot.plot(*denormalized)
But, now I need those keywords. And I'm definitely happy to provide more information if needed. Thanks
The function signature doesn't work the way you think it does. Keyword arguments to matplotlib's plot
function apply to all the lines you specify:
If you make multiple lines with one plot command, the kwargs apply to all those lines, eg:
plot(x1, y1, x2, y2, antialised=False)
If you want to specify individual colors to each line, you need to turn them into format strings that you can pass as every third positional argument. Perhaps you can format them as HTML style hex codes: #RRGGBB
Or alternately, call plot
once per line and pass your color
tuple just once as a keyword argument.
Short answer: No.
Longer answer: Depends on exactly what you are trying to do. The Python interface cannot accept the signature you want, so what is the function, and what are you actually trying to do?
There are several reasons that prevents you to do what you are trying to do:
color=something, ..., color=other
raises an exception x1, y1, color=something, x2
is an error. Even if this worked as you expected, there's still matplotlib
s documentation that states:
If you make multiple lines with one plot command, the kwargs apply to all those lines
Ie you cannot use color=
for only one of the lines, or once for each line. It's a "global" property. You have to use the other ways of providing line colors if you want to specify a different color for each line.
I believe, by your question, that you do not have clear how positional and keyword arguments work so I'll try to give you a clue in this regard.
First of all, there are different kind of parameters. I shall introduce an example to explain the differences:
def a_function(pos_kw1, pos_kw2, *args, kw_only)
This function has:
pos_kw1
, pos_kw2
which can be assigned both by a positional argument or a keyword argument *args
that can be specified only with positional arguments kw_only
that can be specified only with a keyword argument Note: default values have nothing to do with being keyword parameters. They simply make the parameter not required.
To understand the mechanics of argument passing you can think as (although it's not strictly true) if when python performs a function call (eg):
a_function(1, 2, *'abc', kw_only=7)
It first collects all positional arguments into a tuple. In the case above the resultant tuple would be pos_args = (1, 2, 'a', 'b', 'c')
, then collects all keyword arguments into a dict
, in this case kw_args = {'kw_only': 7}
, afterwards, it calls the function doing:
a_function(*pos_args, **kw_args)
Note: since dict
s are not ordered the order of the keywords doesn't matter.
In your question you wanted to do something like:
plot(x, y, color=X, x2, y2, color=Y, ...)
Since the call is actually using *pos_args
and **kw_args
the function:
color=X
was specified right after y
. color=Y
was specified right after y2
. color=X
was specified before color=Y
. Corollary: you cannot specify the same argument more than once since python has no way to know which occurrence should be assigned to which parameter. Also when defining the function you simply couldn't use two parameters with the same name. (And no, python does not automatically build a list of values or similar. It simply raises an error.)
You can also think that python first expands *pos_args
without taking keyword arguments into account, and after that it expands **kw_args
. If you think in this terms you can clearly understand that a function call such as:
# naive intent: assign pos_kw1 via keyword and pos_kw2 via positional
# assuming python will skip positional that were already provided as keyword args
a_function(1, pos_kw1=2)
# or even:
a_function(pos_kw1=2, 1) # hoping order matters
doesn't have any sense because the 1
is assigned to pos_kw1
via positional arguments, and when expanding the keyword arguments it would be reassigned.
Explained in an other way, in the call a_function(*pos_args, **kw_args)
the *pos_args
is a simple tuple-unpacking operation, equivalent to:
pos_kw1, pos_kw2, *args = pos_args
(in python2 you cannot use the *
, but that's how the *args
parameters work more or less).
Tuple-unpacking doesn't skip elements: it simply assign to consecutive elements of the tuple, and so do function calls: there is no check if a positional argument was already passed via keyword and eventually it's skipped. They are simply assigned, blindly.
Due to these restrictions it doesn't make any sense to allow function calls where positionals appear after keyword arguments hence you cannot do something like:
plot(x, y, color=X, x2, ...)
Allowing such function calls would only trick people making them think that order matters for keywords or that arguments could be skipped when unpacking etc. so Python simply raises an error and avoids this kind of ambiguity.
链接地址: http://www.djcxy.com/p/18180.html