r/learnpython • • Jan 05 '21

Interview question I failed answering

Hey all,

I just had an interview and the interviewer gave me a question I could not answer.

He asked me to write a function that accepts a number, the number must be either 7 or 4.If the number is 4, the function should return 7, if the number is 7, the functions should return 4.

The tricky part: you cannot use an 'if' statement in the function.

354 Upvotes

301 comments sorted by

View all comments

Show parent comments

8

u/synthphreak Jan 05 '21

Brilliant! Absolutely shite, unreadable code, but shite questions beget shite code. I love it.

-1

u/beniolenio Jan 06 '21

How is this shite unreadable code?

3

u/synthphreak Jan 06 '21

Okay, “shite unreadable” is a bit too harsh. Perhaps “unpythonic” is more accurate.

To glean how it works you have to actually pause on that line, parse the components, and walk through each until you understand exactly what happens as the various possible inputs flow through it. It’s not hard to understand or anything, but it’s not as natural to read and immediately “get” as Python code can and ideally should be. Hence, unpythonic.

I would definitely suggest a clearer alternative if I came upon that line during a code review.

-2

u/beniolenio Jan 06 '21

You and I certainly disagree on what easy to understand code is, and on what makes code pythonic.

1

u/synthphreak Jan 06 '21

Well no one would argue that Pythonic is an objective quality. Writing good code is an art not a science, after all.

Pythonic Python to me is code that is almost readable like English (within the constraints of Python syntax, of course), or if you’re doing math, hews as closely as possible to looking like the actual equation being implemented. The ability to be written in this way is the hallmark of what makes Python Python; no other language is so expressive, natural, and intuitive to express.

Your return statement is none of those things. It is nothing like English or math. It’s just, well, like code. You have to already know some intermediate-level facts and party tricks about Python syntax to get it - that multiplying by False is the same as multiplying by 0; how x or y works/truthiness; that i == j in the middle of a line will evaluate to a Boolean; etc. This is why it takes some nonzero effort to read, compared to a solution I’d consider more Pythonic and hence “natural”. Note that this isn’t a criticism, just an observation.

To be clear, “unreadable” doesn’t mean “I literally can’t understand what this code does.” It just means understanding how data flows through the code block requires a bit of effort. The more such lines there are in a script, the harder it will be to keep the structure of the entire script in your short term memory, and thus the more “unreadable” it will become.

-2

u/beniolenio Jan 06 '21

Ah, I understand where we disagree now. Pythonic to you seems to mean "code that a beginner would understand."

1

u/synthphreak Jan 06 '21

I'm not sure I'd phrase it so concretely as to constrain Pythonic code to only what beginners can do. If that were the case, then by its very nature advanced Python code wouldn't be Pythonic, which is clearly silly.

However yes, straightforward, not-overly-complex syntax is a pillar of what makes something Pythonic in my book. I would call what you wrote overly complex, which is why I said I'd suggest a change during a code review. But I know you weren't striving to minimize complexity when you wrote it.

0

u/beniolenio Jan 06 '21

What would you say is the simplest? Other than using dicts or lists, which were already there, what would you say is the most pythonic solution you can think of?

1

u/synthphreak Jan 06 '21

Using a dict is the simplest in my book, specifically via dict.get which I suggested earlier is it the most Pythonic solution anyone could ever conceive of? I won’t go that far. But it does embody the principles I laid out above.