[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [Discuss-gnuradio] Python question
From: |
Tom Rondeau |
Subject: |
RE: [Discuss-gnuradio] Python question |
Date: |
Fri, 5 Jan 2007 00:41:02 -0500 |
> -----Original Message-----
> From: address@hidden [mailto:discuss-
> address@hidden On Behalf Of Dan Halperin
> Sent: Thursday, January 04, 2007 11:56 PM
> To: address@hidden
> Subject: [Discuss-gnuradio] Python question
>
> Hi,
>
> I know this isn't really a gnuradio question, but a half-hour on Google
> and browsing the Python site didn't find me the answer. What does ** in
> Python do? Not in the exponent context (2 ** 4 = 16). In particular, I'm
> looking at receive_path.py in the digital examples folder, the following
> code:
>
> # Get demod_kwargs
> demod_kwargs = \
> self._demod_class.extract_kwargs_from_options(options)
>
> ...
>
> # receiver
> self.packet_receiver = \
> demod_pkts(fg,
> self._demod_class(fg, **demod_kwargs),
> access_code=None,
> callback=self._rx_callback,
> threshold=-1)
>
> Is this some crazy C that SWIG does? Thanks,
Nope, just a fun feature Python provides. It allows you to pass a dictionary
object as the arguments to a function.
Say we have a function prototype:
def foo(a, b, c):
We normally call this r = foo(<value_a>, <value_b>, <value_c>), but we could
just as easily call this as r = foo(b=<value_b>, c=<value_c>, a=<value_a>).
What we're doing in the receive_path is setting up a dictionary that would
look like (in the above example) di = {"a": <value_a>, "b": <value_b>, "c":
<value_c>} and calling foo as ret = foo(**di).
It allows us to create different modulators that have different arguments
they require. The "self._demod_class.extract_kwargs_from_otpions" formats
the dictionary for the specific modulation class being used, and that's then
passed to the modulator object. Makes for nice, flexible code.
Tom