[Tutor] Don't understand this class/constructor call syntax
Dave Angel
d at davea.name
Sat Jul 23 01:23:00 CEST 2011
On 07/22/2011 06:40 PM, dave wrote:
> Hello,
>
> I'm trying to work on GNU Radio and having trouble understanding some of the
> Python code. I have a C/C++ coding background. I'm looking at the
> ieee802.15.4 code found on CGRAN. It's about 4 years old and runs but doesn't
> function anymore so I'm trying to fully understand it to fix it.
>
> In one file (a file in src/example called cc2420_txtest.py) I have the
> following line from a constructor for:
>
> class transmit_path(gr.top_block)
> ...
> ...
> ...
> self.packet_transmitter = ieee802_15_4_pkt.ieee802_15_4_mod_pkts(self,
> spb=self._spb, msgq_limit=2)
>
>
>
> Now in the src/python directory for this project I have ieee802_15_4pkt.py
> which has the following class:
>
>
>
> class ieee802_15_4_mod_pkts(gr.hier_block2):
> """
> IEEE 802.15.4 modulator that is a GNU Radio source.
> Send packets by calling send_pkt
> """
> def __init__(self, pad_for_usrp=True, *args, **kwargs):[/code]
>
>
>
> What I don't understand is the call to the constructor and the constructor
> definition. Since it's using a number of advanced features, I'm having
> trouble looking it all up in documentation.
>
> What does it mean to call with spb=self._spb? In the example file, spb is set
> = to 2 and so is self._spb. Is it a sort of pass by reference like C while
> also assigning a value? Why the ** on kwargs then? as if it is a matrix
>
> (and does anyone have any idea what kwargs are (as opposed to args)?)
>
> I'm uncertain about the first argument, but I guess it must be the
> transmit_path object passed in place of the usually implicit self... I'm just
> not sure how Python figures out that it's not pad_for_usrp... magic I guess!
>
>
There are many different things in your question, and I don't know if I
can hit them all in my reply, but I'll see what I can do.
The '=' syntax means an entirely different thing in the call from what
it means in the definition of a function. And the * and ** syntax are
special, although there at least there's a relationship between what
they mean in the two locations. Let's just address these 4 cases
outside of a class, and defer your class questions to when you can post
enough code to see all the pieces (you don't show method
ieee802_15_4_mod_pkts, but it's probably got a @class decorator in front of it, which changes the 'self' convention to a 'cls' one)
When a function definition uses an "=" in it, it's setting a default argument. So if I define a function:
def test(a, b=24):
print a+b
it can be called with either one argument or with two, and if one argument, the value b is implicitly passed. This is similar to C++, except that the default value is initialized at the definition site, and at the time the definition is done. So if the value is mutable, the same value may be used for multiple calls to the same function.
If a function call is made using a similar syntax, it refers to a keyword argument. By default arguments are positional, meaning that they have to be given in the right order, with no gaps. A keyword argument allows you to specify parameters out of order, and by name. It's most useful when there is a function with lots of arguments, most of which you're happy with the default values. To make a trivial example:
def test2(a, b=2, c=3, d=4):
print a,b,c,d
test2(12, d=99)
will print 12 2 3 99
Sometimes you want to pass a lot of arguments without spelling them out
in your call. So if the values are in a list, and in the right order,
you can do something like:
myargs = [3, 99, 12, 44]
test2(*args)
You can also use a dictionary, like:
mykwargs = { "a":3, "d":44, "b":99, "c":12}
test2(**args)
And there's a lot of symmetry when defining a function with those same
similarities:
def test3(*args, **kwargs):
xxxxx
here args will be a list() with the positional arguments, while kwargs
will be a dict() with the keyword ones.
Now these features can be mixed in lots of ways, but that's way too much
for a single message.
(All code above is untested; please forgive any typos)
--
DaveA
More information about the Tutor
mailing list