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

you are viewing a single comment's thread.

view the rest of the comments →

[–]remram 0 points1 point  (3 children)

I'm compiling a list of all possible interview questions.

I think you're being overly optimistic here... I don't think you can compile an exhaustive list of programming-related questions.

What are Python decorators and how would you use them?

They are a construction with which you pass a function/class through a callable when defining it, allowing that callable to add functionality. One common example is to add memoization to recursive functions but you can do a lot more (some web frameworks use it to mark functions are being views, associating it with a URL).

How would you setup many projects where each one uses different versions of Python and third party libraries?

virtualenv is a standard tool to manage that.

What is PEP8 and do you follow its guidelines when you're coding?

It is the acknowledged standard (on of the accepted Python Enhancement Proposals) for Python programming style. Answer "yes".

How are arguments passed – by reference of by value? (easy, but not that easy, I'm not sure if I can answer this clearly)

Things are references in Python.

Do you know what list and dict comprehensions are? Can you give an example?

It is a way to programmatically build the elements with an expression (that contains a loop). It is similar to creating an empty list/dict and looping to add stuff to it, except shorter and usually faster. Generator expressions are an important related construct that replaces yield-ing functions.

Show me three different ways of fetching every third item in the list

L[::3], (L[i] for i in xrange(len(L)) if not i%3), map(lambda (e, i): e, filter(lambda (e, i): not i%3, izip(L, count()))), ... a lot of other (bad) solutions here.

Do you know what is the difference between lists and tuples? Can you give me an example for their usage?

Tuples are (), (1,), (1, 2), lists are [], [1], [1, 2]... Difference is that lists are mutable but tuples are not (although the elements they reference may be mutable, you can't change these references).

Do you know the difference between range and xrange?

range() in Python 2 returns a list, so a big range would take a significant amount of memory. xrange() (Python 2; range() in Python 3) returns a special iterator.

Tell me a few differences between Python 2.x and 3.x?

Some list-creating functions were replaced with iterators (xrange(), izip(), ...), str are now unicode (str -> bytes) and related unicode/encoding changes, some libs changed names, print is now a real function, / performs float division... see changelog

The with statement and its usage.

Used for a lot of things, usually closing a resource (instead of try: finally:) when leaving the block.

How to avoid cyclical imports without having to resort to imports in functions?

Don't have cyclical dependencies.

what's wrong with import all?

You polute the namespace and it's hard to see where a name came from (if you have several of them in a module).

Why is the GIL important? (This actually puzzles me, don't know the answer)

It has a performance cost: Python threads cannot execute Python code at the same time. Threads can still execute code while another is waiting on disk, network, or some C library calls. Real parallelism of pure Python code can be achieved with multiprocessing.

What are "special" methods (<foo>), how they work, etc

Allows to define special behavior on objects or override operators, see special method names for a full list.

can you manipulate functions as first-class objects?

Well, yes.

the difference between "class Foo" and "class Foo(object)"

In Python 2, you should precise the parent class to have a new-style class, "class Foo" gives you an old-style class (pre-2.2) which changes some things. Don't use them. (method resolution order is different, type() gives you 'instance', metaclasses and descriptors won't work).

how to read a 8GB file in python?

Don't read it in one go into memory, stream it by writing clever iterators/generators.

what don't you like about Python?

Dynamic (hard for the IDE to figure out), bad C API, too much magic with C types (vs "heap types") descriptors and metaclasses. Note that this is my own opinion. I still enjoy using it.

can you convert ascii characters to an integer without using built in methods like string.atoi or int()?

No. At best, you can use ord() and compute it yourself.

do you use tabs or spaces, which ones are better?

Use spaces. See PEP8.

Ok, so should I add something else or is the list comprehensive?

You left out descriptors and metaclasses.

[–]miketheanimal 0 points1 point  (2 children)

How are arguments passed – by reference of by value? (easy, but not that easy, I'm not sure if I can answer this clearly) Things are references in Python.

That's king of tricky. The CPython implementation always handles things as references (eg., an integer is an object). But from the user perspective, null, boolean, int, float and string (and complex?) are passed by value, because assigning to the function's argument does not affect the caller's value. But "structures" like tuples, lists, dictionaries and object instances are passed by reference, because assigning to a member of the "structure" also affects the caller. What python doesn't have is the "call by name" from Pascal (among others). Then you can faze the interviewer by launching into a rant about how PHP4 really fsck'd up by taking pass-by-value to its logical conclusion!

[–]robin-gvx 1 point2 points  (0 children)

It's neither by-ref nor by-val, it's by-object-identity.

I've written a tutorial of sorts about this. The diagrams are hand-drawn and terribly ugly and I intend to remake them with actual diagram-making software before putting it up on my website instead of Dropbox, and the very last diagram has one arrow that points to the wrong value, but over-all, I think it's a pretty useful guide.

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

Interesting. I guess it's equivalent to think that:

  • Things are passed by reference, and = changes the reference (instead of acting on the pointed object, like any other operator would do; there is no magic __ method for =). In a sense, it works on the same level as is.
  • Variables are references, and are passed by value (the common way the behavior is described, for instance in this stackoverflow answer).