Mailing List Archive

1 2 3 4 5  View All
Re: Python Interview Questions [ In reply to ]
In article <50aac66c$0$29983$c3e8da3$>,
Steven D'Aprano <> wrote:

> I'm asking about the case where one might want the key to remain mutable
> even after it is used as a key, but can't because Python won't let you.

Ah. Now I see what you're getting at. Thank you.

Well, I will admit that it probably doesn't make sense to mutate an
object after it's put into a dict (or at least mutate it in a way which
changes it's hash value and/or whether it compares equal to the original
object). If you did (assuming lists were allowed as keys):

l = [1, 2, 3]
d = {l: "spam"}
print d[l]

I'm not sure what I would expect to print. It's not too hard to
experiment, though. All you need do is:

class HashableList(list):
def __hash__(self):
return hash(tuple(self))

and python is then happy to let you use a list as a key. I just played
around with this a bit off-line. I think I got the results I was
expecting, but since I'm not sure what I was expecting, that's hard to

However, you didn't ask if it made sense to mutate an object after using
it as a key. You asked if it made sense to let the object remain
mutable after using it as a key. That's a harder question.

Let's say I had lots of of lists I wanted to use a dictionary keys. As
it stands now, I have to convert them to tuples, which means copying all
the data. For a lot of data, that's inefficient.

Wouldn't it be nice (or at least, more efficient) if I could just use
the original lists as keys directly, without the extra copy? I would
have to understand that even though they are mutable, interesting (and
perhaps, unwanted) things if I actually mutated them. But, we're all
consenting adults here. If I'm willing to accept responsibility for the
consequences of my actions in return for the efficiency gain, why
shouldn't I be allowed to?

I guess the answer is, that I am allowed to. I just need to do the
HashableList deal, shown above (no broken URL required to read the code).
Re: Python Interview Questions [ In reply to ]
On Tuesday, October 30, 2007 12:24:04 PM UTC-7, Tim Chase wrote:
> > I have used Python for a couple of projects last year and
> > I found it extremely useful. I could write two middle size
> > projects in 2-3 months (part time). Right now I am a bit
> > rusty and trying to catch up again with Python.
> >
> > I am now appearing for Job Interviews these days and I am
> > wondering if anybody of you appeared for a Python
> > Interview. Can you please share the questions you were
> > asked. That will be great help to me.
> While I haven't interviewed precisely for Python, I've been
> on the other (interviewing) end and can offer a few of the
> sorts of things I ask. I don't expect perfect answers to
> all of them, but they show me a range of what the
> interviewee knows. I try and give a scattershot of
> questions from the following areas to try and narrow down
> where they fall in terms of pythonability, and then grill
> more deeply around the edges that I find.
> Basic Python:
> =============
> - do they know a tuple/list/dict when they see it?
> - when to use list vs. tuple vs. dict. vs. set
> - can they use list comprehensions (and know when not to
> abuse them? :)
> - can they use tuple unpacking for assignment?
> - string they use "+=" or do they build a list
> and use .join() to recombine them efficiently
> - truth-value testing questions and observations (do they
> write "if x == True" or do they just write "if x")
> - basic file-processing (iterating over a file's lines)
> - basic understanding of exception handling
> Broader Basic Python:
> =====================
> - questions about the standard library ("do you know if
> there's a standard library for doing X?", or "in which
> library would you find [common functionality Y]?") Most
> of these are related to the more common libraries such as
> os/os.path/sys/re/itertools
> - questions about iterators/generators
> - questions about map/reduce/sum/etc family of functions
> - questions about "special" methods (__<foo>__)
> More Advanced Python:
> =====================
> - can they manipulate functions as first-class objects
> (Python makes it easy, but do they know how)
> - more detailed questions about the std. libraries (such as
> datetime/email/csv/zipfile/networking/optparse/unittest)
> - questions about testing (unittests/doctests)
> - questions about docstrings vs. comments, and the "Why" of
> them
> - more detailed questions about regular expressions
> - questions about mutability
> - keyword/list parameters and unpacked kwd args
> - questions about popular 3rd-party toolkits (BeautifulSoup,
> pyparsing...mostly if they know about them and when to use
> them, not so much about implementation details)
> - questions about monkey-patching
> - questions about PDB
> - questions about properties vs. getters/setters
> - questions about classmethods
> - questions about scope/name-resolution
> - use of lambda
> Python History:
> ===============
> - decorators added in which version?
> - "batteries included" SQL-capible DB in which version?
> - the difference between "class Foo" and "class Foo(object)"
> - questions from "import this" about pythonic code
> Python Resources:
> =================
> - what do they know about various Python web frameworks
> (knowing a few names is usually good enough, though
> knowledge about the frameworks is a nice plus) such as
> Django, TurboGears, Zope, etc.
> - what do they know about various Python GUI frameworks and
> the pros/cons of them (tkinter, wx, pykde, etc)
> - where do they go with Python related questions (c.l.p,
> google, google-groups, etc)
> Other Process-releated things:
> ==============================
> - do they use revision control
> (RCS/CVS/Subversion/Mercurial/Git...anything but VSS) and
> know how to use it well
> - do they write automated tests for their code
> Touchy-feely things:
> ====================
> - tabs vs. spaces, and their reasoning
> - reason for choosing Python
> - choice of editor/IDE
> Good luck with your interviewing and hope this helped,
> -tkc

I appreciate all these. I thought I knew Python!
Re: Python Interview Questions [ In reply to ]
Somebody, whose identity has been lost in three-deep quoting, said:
> > > I am now appearing for Job Interviews these days and I am
> > > wondering if anybody of you appeared for a Python
> > > Interview. Can you please share the questions you were
> > > asked. That will be great help to me.

We have a standard list of about 2 dozen screening questions we use that
cover a broad but shallow swath of CS, Unix, and Python. I'm not going
to share the exact questions, but here's some of the Python topics we

The ramifications of string immutability.

How default function arguments work, especially how they interact with
mutable objects.

How booleans and various ways of testing for equality work.

A question about subclassing a built-in type.

List comprehensions vs. generator expressions.

We don't expect everybody to get every question, but it gives us a quick
first cut to evaluate applicants before we decide to bring them in for
an interview or not.

We also want to see that you understand some basic computer science. If
nothing else, you need to understand, what O(n) means, and be able to
give some examples of Python code which exhibit various orders of

1 2 3 4 5  View All