-3

My PR will get denied if I try merge this.

How can I refactor this to be more elegant/shorter/have more meaning?

if len(my_list) < 2:
    raise Error("No List to preview")
if license_metadata[0]["active"] 
    active = ListSetting(**my_list[0]                                                   
    preview = ListSetting(**my_list[1])
else:
    active = ListSetting(**my_list[1] .                           
    preview = ListSetting(**my_list[0])

Sorry I had to make var names generic. Codebase is private & under NDA.

Dean Coakley
  • 1,675
  • 2
  • 11
  • 25
  • Your code is not indented correctly. [About the ** thing, here is a good answer.](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – Arne Feb 14 '18 at 08:35
  • Why downvote, need more context? – Dean Coakley Feb 14 '18 at 08:35
  • Probably because you violate a few points on the [how to ask a good question](https://stackoverflow.com/help/how-to-ask) chart. Making var names generic is actually a good idea, in the sense of creating an [MCVE](https://stackoverflow.com/help/mcve). – Arne Feb 14 '18 at 08:36
  • @Arne Still don't really see what's wrong with my question to be honest. Thanks for the `**` answer, that was very helpful. – Dean Coakley Feb 14 '18 at 08:43
  • 1): bad formatting. It's just unnecessary, you can preview your post and check if it's formated correctly. Many people will just stop reading your question if they can't even copy paste your code. 2): asking two questions in one question. Just don't. 3): low effort. If you had entered your `**` verbatim into google, you would have gotten the answer I linked as the top result - why didn't you? 4): In the vein of the first point, you also lack a reproducible example. – Arne Feb 14 '18 at 08:54
  • 1
    I hope I didn't come off as harsh, you asked what was wrong, I am honestly trying to answer that question. – Arne Feb 14 '18 at 08:55
  • 1. oh, even after you mentioned it initially I couldn't see the error myself. Only noticed it now. My bad. 2. Somewhat fair. I agree I prefer if it doesn't happen but not a big deal. 3. Clearly I'm bad at searching. But yeah after looking through 3 posts I found similar answer. 4. Interesting, I thought it was unnecessary. (Still somewhat think this way) - Not harsh, you were quite objective, thanks again – Dean Coakley Feb 14 '18 at 09:05

1 Answers1

1
if len(my_list) < 2:
    raise Error("No List to preview")
a = ListSetting(**my_list[0])
b = ListSetting(**my_list[1])
active,preview = (a, b) if license_metadata[0]["active"] else (b, a)

you can give variable a and b signification names in your context.

** is keyword argument dictionary, Here is a reference for someone else has explained What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

potaty
  • 26
  • 5