Reputation: 23
I'm wondering what is actually trying to happen in the following line. Forgive me I'm new to python syntax, thanks for the help!
Handlers[type(dbw_msg)](dbw_msg)
Where handlers = secondary: self.secondary_manage.handle.message
and dbw_msg = py_from_can(can_msg)
where py_from can is defined as
def py_from_can(can_msg):
try:
if can_msg.ID in py_can_types:
return py_can_types[can_msg.ID].py_from_can(can_msg)
except NotImplementedError:
pass
return None
Upvotes: 2
Views: 74
Reputation: 95948
So, without any other context, all we can say is:
Handlers[type(dbw_msg)](dbw_msg)
Handlers
gets subscripted (i.e. the square-brackets) with type(db_msg)
. We can only assume this is some sort of mapping (a dict
) where the keys are type
objects.
Finally, the value that is returned by the subcription operation get's called, i.e. the ()
parens. So, Handlers
is some mapping from type
objects to a callable (e.g. functions).
So, for example:
>>> handlers = {int: lambda x: x**2, str: str.upper}
>>> a = 8
>>> b = 'foo'
>>> handlers[type(a)](a)
64
>>> handlers[type(b)](b)
'FOO'
Note, str.upper
is merely the .upper
method from your normal strings, i.e.:
>>> "baz".upper() # note the call!
'BAZ'
Is the same as:
>>> str.upper("baz")
'BAZ'
And a lambda
is merely a way to write anonymous functions. It is equivalent to:
def square(x):
return x**2
Upvotes: 1