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,

-Dan

On Thu, Jan 04, 2007 at 08:55:56PM -0800, Dan H. wrote:

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:

As Tom said it allows dictionaries to be used for passing argument
lists. Details on both * and ** can be found in the Python Language
Reference here: http://docs.python.org/ref/function.html

Eric

and browsing the Python site didn’t find me the answer. What does ** in
# 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs