We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
if len(inp) > 1:
getattr(d, inp[0])(inp[1])
else:
getattr(d, inp[0])
In order to get it all on one line, you have to use a x if y else z form statement, so you want to say getattr(d, inp[0])(inp[1] if len(inp) > 1 else something) because you have to put an else something at the end of the statement, and it has to be an expression, so the easiest way to get nothing is to use the star operator on an empty list, because the star operator takes the contents of a list out of their list, so we have inp[1] if len(inp) > 1 else *[] but that is invalid syntax, because the asterisk has to go at the beginning of the x if y else z, so we have to put inp[1] in a list too, leaving getattr(d, inp[0])(*[inp[1] if len(inp) > 1 else *[])
Collections.deque()
You are viewing a single comment's thread. Return to all comments →
It is equivalent to:
In order to get it all on one line, you have to use a
x if y else z
form statement, so you want to saygetattr(d, inp[0])(inp[1] if len(inp) > 1 else something)
because you have to put anelse something
at the end of the statement, and it has to be an expression, so the easiest way to get nothing is to use the star operator on an empty list, because the star operator takes the contents of a list out of their list, so we haveinp[1] if len(inp) > 1 else *[]
but that is invalid syntax, because the asterisk has to go at the beginning of thex if y else z
, so we have to putinp[1]
in a list too, leavinggetattr(d, inp[0])(*[inp[1] if len(inp) > 1 else *[])
Let me know if you need more clarification.