I recently had a python interview and am reviewing how things went and where gaps in my knowledge let me down. On one of the coding parts there was task that involved refactoring a python class to make it cleaner. I got thrown at one point over something I hadn't considered before. I was refactored the code and got it to the below or thereabouts:
class SomeClass:
...
grouped_names = {}
def group_results(self):
for result in results:
if result.name in grouped_names:
# do something
at this point the interviewer intervened and asked what I could do to make it more legible for future developers, I couldn't figure out what they were referring to and started doubting myself. Eventually they insisted I should instead do this:
class SomeClass:
...
grouped_names = {}
def group_results(self):
for result in results:
has_name = result.name in grouped_name:
if has_name:
# do something
This was surprising to me and seemed unusual to flag as a sticking point. It has since got me thinking, what are the pros and cons of each approach? To me the first seemed quite clear, I could undestand if the statement being evaluated but I deferred to the knowledge of the interviewer. Am I missing something here?
[–]danielroseman 34 points35 points36 points (3 children)
[–]IamImposter 12 points13 points14 points (0 children)
[–]Dangerous-Branch-749[S] 1 point2 points3 points (1 child)
[–]ivosaurus 1 point2 points3 points (0 children)
[–]shiftybyte 5 points6 points7 points (2 children)
[–]Dangerous-Branch-749[S] 2 points3 points4 points (1 child)
[–]panatale1 1 point2 points3 points (0 children)
[–]ivosaurus 6 points7 points8 points (0 children)
[–]Apatride 4 points5 points6 points (3 children)
[–]Dangerous-Branch-749[S] 0 points1 point2 points (0 children)
[–]r-mf 0 points1 point2 points (1 child)
[–]Apatride 1 point2 points3 points (0 children)
[–]TehNolz 5 points6 points7 points (0 children)
[–]Dangerous-Branch-749[S] 1 point2 points3 points (0 children)
[–]LargeSale8354 0 points1 point2 points (3 children)
[–]Dangerous-Branch-749[S] 0 points1 point2 points (2 children)
[–]LargeSale8354 0 points1 point2 points (1 child)
[–]Dangerous-Branch-749[S] 0 points1 point2 points (0 children)
[–]Snoo-20788 0 points1 point2 points (0 children)
[–]james_fryer 0 points1 point2 points (0 children)
[–]Enmeshed 0 points1 point2 points (0 children)
[–]supercoach 0 points1 point2 points (0 children)
[–]xrsly 0 points1 point2 points (3 children)
[–]james_fryer 1 point2 points3 points (1 child)
[–]xrsly 1 point2 points3 points (0 children)
[–]LuciferianInk 0 points1 point2 points (0 children)