pythoon_interview_redit
easy/intermediate
What are Python decorators and how would you use them?
How would you setup many projects where each one uses different versions of Python and third party libraries?
What is PEP8 and do you follow its guidelines when you're coding?
How are arguments passed – by reference of by value? (easy, but not that easy, I'm not sure if I can answer this clearly)
Do you know what list and dict comprehensions are? Can you give an example?
Show me three different ways of fetching every third item in the list
Do you know what is the difference between lists and tuples? Can you give me an example for their usage?
Do you know the difference between range and xrange?
Tell me a few differences between Python 2.x and 3.x?
The with statement and its usage.
How to avoid cyclical imports without having to resort to imports in functions?
what's wrong with import all?
Why is the GIL important? (This actually puzzles me, don't know the answer)
What are "special" methods (<foo>), how they work, etc
can you manipulate functions as first-class objects?
the difference between "class Foo" and "class Foo(object)"
tricky, smart ones
how to read a 8GB file in python?
what don't you like about Python?
can you convert ascii characters to an integer without using built in methods like string.atoi or int()? curious one
subjective ones
do you use tabs or spaces, which ones are better?
Ok, so should I add something else or is the list comprehensive?
[–]d4rch0nPythonistamancer 50 指標 3 年前
I'll try my hand at a few:
1 What are Python decorators and how would you use them?
They extend past python, and are functions that take a function as an argument and return functions. A simple example might be a decorator that takes a function, prints its args to stdout, prints the return value to stdout, then returns that return value. The syntax in Python is usually done with the @decorator_name above a function definition.
2 How would you setup many projects where each one uses different versions of Python and third party libraries?
virtualenv
3 What is PEP8 and do you follow its guidelines when you're coding?
A coding standard, and I try to. pylint is a great help.
4 How are arguments passed – by reference of by value?
Probably all through reference, but I'm not sure about primitives under the hood. Anyone know this? If you pass f(12, 81), are those by value?
5 Do you know what list and dict comprehensions are? Can you give an example?
ways to construct a list or dict through an expression and an iterable.
>>> x = [(a, a+1) for a in range(5)]
>>> y = dict((a,b) for a,b in x)
>>> x
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
>>> y
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5}
6 Show me three different ways of fetching every third item in the list
[x for i, x in enumerate(thelist) if i%3 == 0]
for i, x in enumerate(thelist):
if i % 3: continue
yield x
a = 0
for x in thelist:
if a%3: continue
yield x
a += 1
7 Do you know what is the difference between lists and tuples? Can you give me an example for their usage?
Tuples are immutable. A tuple might be a good type for a coordinate inst var in some class. Lists are ordered collections, but with a tuple, each index generally has a certain meaning, so coord[0] is the x coordinate and coord[1] is y.
8 Do you know the difference between range and xrange?
Range returns a list of the full sequence while xrange generates each element iteratively like you would with the "yield" keyword. This changes in python3, and the default behavior is to yield like xrange. I think xrange is out.
9 Tell me a few differences between Python 2.x and 3.x?
The previous answer. print is no longer a statement and is just a function ("print 5" won't work anymore and you need parens), they added the Ellipse object (...). That's all I know off hand.
10 The with statement and its usage.
It's for context management, and you can define your own that implement enter init and exit if it might help. This is very useful for opening and closing files automatically (with open(foo) as bar:)
11 How to avoid cyclical imports without having to resort to imports in functions?
Refactoring your code? Not sure. When I've ran into this I generally have restructured functions into different modules which ended up cleaning everything anyway.
12 what's wrong with import all?
You can overwrite functions and this can be dangerous especially if you don't maintain that module.
rewrite.py def open(foo): print('aint happening!')
test.py from rewrite import * z = open('test.txt')
prints aint happening!
13 Why is the GIL important?
It has to do with preventing true multithreaded bytecode, and has been an issue forever. I think python bytecode execution is protected with the Global Interpreter Lock so every bc execution is atomic. Explained best here: http://wiki.python.org/moin/GlobalInterpreterLock
You might want to consider writing a multithreaded module or program in C and wrapping it with Python if this is an issue for you.
14 What are "special" methods (<foo>), how they work, etc
These are methods like str and gt, which override behavior of other global functions like str() and operators like >. enter and exit will be used with the with keyword, and there are many more like getattr. Overriding getattr can result in some very unpredictable behavior with a dynamic language like Python, and you should be very careful when you use magic like that.
15 can you manipulate functions as first-class objects?
Yes. eg. they can be passed as args to functions.
16 the difference between "class Foo" and "class Foo(object)"
class Foo(object) inherits from the new-style object. I don't know the specifics, but here's stack overflow: http://stackoverflow.com/questions/4015417/python-class-inherits-object
17 how to read a 8GB file in python?
Operate on chunks, and not one byte at a time. Be wary about the RAM of the host machine. What is the nature of the data such that it is so large? How are you operating on it? What are you returning? Are you accessing it sequentially or randomly? There's a lot more to ask than to answer here.
18 what don't you like about Python?
It's slow, and it can be too dynamic for certain tasks in my opinion. It is not compiled. It can be very unpredictable. People abuse the flexibility of it sometimes.
19 can you convert ascii characters to an integer without using built in methods like string.atoi or int()? curious one
struct.unpack("<I", foo)[0]
ord, chr
20 do you use tabs or spaces, which ones are better?
Spaces. Stick to PEP8 when possible.
Ok, so should I add something else or is the list comprehensive?
generators/yield keyword
what is multiple inheritance / does python have multiple inheritance
is Python compiled, interpreted and/or emulated
What differentiates Python from Ruby
How do you debug your Python? What's pdb and how do you use it?
How do you modify global variables in a function? Why should you avoid this?
Use of the re module... what is it, give an example, etc.
pythoon_interview_redit的更多相关文章
随机推荐
- 8.Django模型类例子
这里定义4个模型 作者:一个作者有姓名 作者详情:包括性别,email,出生日期, 出版商:名称,地址,城市,省,国家,网站 书籍:名称,日期 分析: 作者详情和作者一对一的关系 一本书可以有多个作者 ...
- Android Message handling (based on KK4.4)
一.几个关键概念 1.MessageQueue:是一种数据结构,见名知义,就是一个消息队列.存放消息的地方.每个线程最多仅仅能够拥有一个MessageQueue数据结构. 创建一个线程的时候,并不会自 ...
- proguard 不混淆第三方jar包的问题
导入lib文件,并且用-dontwarn 和 keep class结合试试,例如: -libraryjars libs/log4j.jar -dontwarn org.apache.log4j.*-k ...
- R语言图形base系统(三)
本篇介绍R语言base系统绘制散点图.条形图.直方图.箱线图.饼图,还将简单介绍点图.核密度图.折线图. 散点图: attach(mtcars) plot(wt, mpg, main="B ...
- log4j2.xml的例子
项目中用到的一个log4j2.xml的配置文件: <?xml version="1.0" encoding="UTF-8"?> <!--设置l ...
- Data Structure Graph: strong connectivity
如果为undirected graph就是dfs或者bfs,如果都能visit则为连通O(V+E). 如果为directed graph就先dfs或者bfs,再reverse direct,再dfs或 ...
- python中的id
python对象都有三个特性分别是身份.类型.值,身份指该对象内存地址,内建函数id()可获得身份,类似于指针的地址,但不能控制这个值,类型决定对象可以保存什么类型的值,值是对象表示的数据项,pyth ...
- 【leetcode刷题笔记】Single Number
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- grep egrep
grep: Global search REgular expression and Print out the line. 作用: 文本搜索工具,根据用户指定的“模式”对目标文本逐行进行匹配检查:打 ...
- GIT使用[git remove untracked working file]
使用GIT进行merge的时候, git merge --no-ff master 如果merge之后出现问题, 想进行回退, 可以使用 git reset --hard HEAD 来回退到最新的版本 ...