What is this python syntax trying to do? -
i'm wondering trying happen in following line. forgive me i'm new python syntax, 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 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
so, without other context, can is:
handlers[type(dbw_msg)](dbw_msg) handlers gets subscripted (i.e. square-brackets) type(db_msg). can assume sort of mapping (a dict) keys type objects.
finally, value returned subcription operation get's called, i.e. () parens. so, handlers mapping type objects callable (e.g. functions).
so, example:
>>> handlers = {int: lambda x: x**2, str: str.upper} >>> = 8 >>> b = 'foo' >>> handlers[type(a)](a) 64 >>> handlers[type(b)](b) 'foo' note, str.upper merely .upper method normal strings, i.e.:
>>> "baz".upper() # note call! 'baz' is same as:
>>> str.upper("baz") 'baz' and lambda merely way write anonymous functions. equivalent to:
def square(x): return x**2
Comments
Post a Comment