-
Notifications
You must be signed in to change notification settings - Fork 2
Why_is_keyword_order_important_when_creating_nodes?
> On Tue, 13 Mar 2007, Oleg M. Smirnov wrote:
>
>> Ilse van Bemmel wrote:
>>
>>> Second, I have define a 3x3 matrix to multiply with a 3x1 matrix
>>> (vector). The Meq.Composer takes dimensions, but the compiler keeps
>>> stumbling on this. Is this the correct way to call it:
>>> ns.matrix << Meq.Composer(dims=[3,3], comp1, comp2, ... comp9)
>>>
>> No, try Meq.Composer(comp1,comp2,... comp9,dims=[3,3])
>>
>> Cheers,
>> Oleg
>>
>
> Oleg, all the examples you have with the Collections plotter
> generally do something like
>
> ns.collector << Meq.Composer(dims=[0], tab_label = 'XNTD',
> *[ns.G(p) for p in ANTENNAS]);
>
> So what's the difference with the above version?
>
In your example the children are specified via a list (the *-syntax), which in this case is constructed on-the-fly. Going via a list is necessary because we don't know in advance how many antennas there will be, so we have to build a list and pass it in via the * thingy. Ilse could also have written her code using the *-syntax:
Meq.Composer(dims=[3,3],*[comp1,...,comp9]).
The more familiar Meq.Composer(comp1,comp2,... comp9,dims=[3,3]) syntax can only be used if you have a fixed, known number of children (in this case 9).
Ilse's original syntax is incorrect because keyword arguments (dims=) must come after normal arguments. Confusingly enough, if you specify normal arguments via the *-syntax, the order is reversed, and keyword arguments must come FIRST, as in Tony's example. I'm sure there's a good reason why Guido did it this way, but you'll have to dig through the Python documentation to find out... Another thing to watch out for is that if a function is defined as
def f(x=1,*args,**kw):
and you call it as f(z=0,*[1,2,3]), then inside the function x=1, args=(2,3), and kw={'z':0}, which can be a nasty surprise...
Actually, a clearer example would be: if you call it as f(z=0,*[8,9,10]), then inside the function x=8, args=(2,3) and kw={'z':0}.
If you want to get a clear idea of how these things work, just type the following at the Python console:
>>> def a(*args,**kw):
print args
print kw
and then try invoking a() with various combinations of arguments