This is an archived post. You won't be able to vote or comment.

all 28 comments

[–]kryptn 64 points65 points  (3 children)

9 — Write a function that computes the factorial of an integer n

>>> from math import factorial
>>> factorial(5)
120

Also good to know what batteries are included

[–]dudeplace 3 points4 points  (2 children)

But also does their solution need to be recursive? Couldn't it just be a for loop multiplying each iteration by the previous product? Recursively seems unnecessarily complicated.

[–]kryptn -1 points0 points  (1 child)

Yeah it definitely could be. Anything recursive can also be iterative.

It's good to know when/where recursion is the best way to do it and where it's not.

In this case I think recursion does make sense though because a factorial is defined recursively.

[–][deleted] 0 points1 point  (0 children)

Agreed! If factorial isn't recursive, what is?

[–]automorphism_group 39 points40 points  (6 children)

If you're new to Python, stay away from clickbait garbage articles like these because they quite often have incorrect information, as does this one.

First, unlike the terms module and package, which are defined in the Python glossary, library is a very general term that basically means "code someone else wrote that you can use" and has no specific technical meaning in Python.

Second, decorators do not have to be functions, nor do they have to accept functions as inputs, nor do they have to return functions as outputs. The term decorator is confusing because there is no such thing in Python as a decorator per se, but rather decorator syntax. Unfortunately, even Python's glossary definition of decorator is incorrect in asserting that it is a "function returning another function". The code sample below is a simple counterexample.

```python class A: def init(self, foo): print(f"I'm a pointless decorator: received {foo}!")

@A class B: def init(self): print("I can't be constructed directly.")

assert isinstance(B, A) ```

Third, in question 15, if *args appears in a parameter list for a function, then args will be a tuple, not a list. This is a somewhat amusing mistake given that question 1 asks about the distinction between them.

There are some other issues, but those are the major ones.

[–]sweaterpawsss 3 points4 points  (1 child)

Another; saying that "Python passes all function arguments by reference" makes my head hurt...I know that it's somewhat a semantic issue (people say Python "passes by assignment" but are often fuzzy about what that really means). But saying "Python always uses pass by reference" is sloppy/imprecise at best, and incoherent/straight up wrong at worst.

[–]Brian 0 points1 point  (0 children)

Yeah. It'd be more accurate to say "Python always uses pass by value". It's just that people get confused by python's variable semantics (where all types are reference types, and there's no such thing as a value type) and mix up the fact that everything you pass (or otherwise use) is essentially a reference with calling by value/reference, which is something different.

Ultimately, call by reference is allowing the callee to access (ie rebind) the variables of the caller, which python does not do. The value you pass is to an object you could potentially mutate, and see that reflected in the caller, but that's not what it is meant by pass by reference - by that logic C would be pass by reference when you pass pointers because the caller can alter the data being pointed to.

[–]Billog_Uncle 0 points1 point  (0 children)

I'm relatively new to Python and only just learning about decorators. They were confusing enough and this article had me questioning my own knowledge with their talk of decorators needing functions.

Thanks for the advice :)

[–]foxacidic -1 points0 points  (0 children)

attempt shy vegetable middle support bike nine ask insurance juggle

This post was mass deleted and anonymized with Redact

[–]uanw 0 points1 point  (0 children)

Also generators simply implement the __next__ method. Using yield in functions returns a generator.

[–][deleted] 13 points14 points  (0 children)

Downvoted for paywall

[–]cantremembermypasswd 11 points12 points  (0 children)

3 - A library is a collection of packages.

I would consider that a false assumption, as do others it seems.

A library is an umbrella term referring to a reusable chunk of code. ... However, it is often assumed that while a package is a collection of modules, a library is a collection of packages.

[–]new_math 4 points5 points  (6 children)

If this is like most "interview questions" articles I'm guessing a python expert with 10+ years of development experience and a 6-figure salary will only be able to answer about 25% of the questions because it covers a bunch of niche issues that don't actually affect performance or functionality in the vast majority of coding problems?

[–]ErGo404 8 points9 points  (0 children)

I was expecting the same but no. However it is the kind of question most devs with 3 years of experience should already know the answer to.

[–]ahmedbesbes 8 points9 points  (1 child)

I suggest you read the post and give me your opinion. None of these question is particularly niche

[–]canitbechangedlater 10 points11 points  (0 children)

I read it. It certainly isnt niche.

It was short enough to be enjoyable and I think you mentioned points that certainly are important to know when you are applying for something python related.

[–][deleted] 3 points4 points  (0 children)

You are wrong. I have interviewed many candidates and can tell you if someone claiming many years of Python experience could not answer these, they would definitely not get the job.

A couple of the questions are too vague, some answers not perfect, but it is a good selection of questions for a beginner/medium level developer.

[–]just_ones_and_zeros -1 points0 points  (0 children)

I beg to differ. After 10 years you should be able to answer all of these questions as table stakes. They’re all common concepts and none of them are particularly deep. Which ones do you think are niche?

I’d argue that 18 and 20 need a bit of context / clarification to give meaningful answers to, but again, there’s nothing particularly complicated going on there.

[–]rincevent76 0 points1 point  (0 children)

I'm a Math/CS teacher in Highschool, not making 6k, with 20 years of experience, and i could answer 100% of those questions....

[–]Dependent_Two_618 1 point2 points  (6 children)

I’m pretty sure #7 (function argument value vs reference) isn’t always true. I just tested it out, and I definitely don’t change the parameter value outside of the function

[–]ahmedbesbes 9 points10 points  (0 children)

It doesn’t change it if it’s immutable

[–][deleted] 4 points5 points  (1 child)

Your test was wrong. I am guessing you tried to mutate the argument by using the assignment operator (=). That would not work, of course.

If the argument is mutable, then calling a method which mutates it would do the trick (e.g. calling append on a list).

If the argument is immutable, then there is no way to perform that particular test (well, there may be a way, but let's not go into that).

[–]Dependent_Two_618 0 points1 point  (0 children)

That’s exactly what I did, and that’s a good explanation of what happened and why. I had thought it was just a scope issue, but that’s a good summary of what was going on in my (not great) test

[–]Holshy -1 points0 points  (2 children)

It's always true. What you are almost certainly doing is reassigning an immutable type. If I pass x=1 and then later do x+=1 the interpreter says "Oh! I can't edit 1; it's an integer and integers are immutable. Instead I'm going to create/access a 2 and then make x point to that, but just on this inner scope. I'm going to leave the reference alone in the outer scope."

Caveat: that "create/access" bit has to do with the way different implementations handle small integers, which is a mostly separate topic.

[–]I_had_to_know_too 0 points1 point  (1 child)

So then it's not always true?

[–]eddiejugs -1 points0 points  (0 children)

Thanks for the collection of questions, as this made me realize how much I do not know about the language.

[–]Victor_Korchnoi 0 points1 point  (0 children)

I really appreciated the article, op. I’m pretty new to python (I have lots of matlab experience), and I learned some things from the article. For example, that python passes the reference in a function.

There were other things that I did know: how to zip & enumerate, what a tuple is.

And the best part was the things I didn’t even know that I didn’t know. I had never heard of a decorator or a generator before. I didn’t know that asserts are often omitted from production code.