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的更多相关文章

随机推荐

  1. Linux使用yum安装rpm包

    1.yum其实管理的也是rpm包,只不过依赖什么的都自己做了2.yum在有的linux版本是收费的,但是CentOS是免费的3.yum一般意义上是需要联网的,即:使用网络yum源 a.yum源配置文件 ...

  2. JVM垃圾回收时的可触及性

    可触及的 1.从根节点可以触及到这个对象可复活的 1.一旦所有引用被释放,就是可复活状态 2.因为在finalize()中可能复活该对象不可触及的 1.在finalize()后,可能会进入不可触及状态 ...

  3. 安装Nginx 及使用

    1.下载 Nginx wget http://nginx.org/download/nginx-1.10.3.tar.gz   (稳定版) 2.提前下载好依赖包 openssl.zlib.pcre p ...

  4. 使用基本 SQL 命令

    概述 在本教程中,将学习结构化查询语言 (SQL),包括: 使用基本 SQL 命令 执行基本数据操做 数据库和 SQL 在本系列教程中,目前我们使用平面文本文件来存储数据.平面文本文件可能适合相对较少 ...

  5. 1django 视图与网址

    创建一个项目,名字叫mysite django-admin startproject mysite(项目名) 成功后,看到如下样式 mysite ├── manage.py └── mysite ├─ ...

  6. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

  7. jQuery设计理念

    jQuery设计理念 引用百科的介绍: jQuery是继prototype之后又一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1. ...

  8. 【leetcode刷题笔记】Reverse Words in a String

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

  9. Win7打开新的文件夹总会以新窗口的形式打开

    首先可以在 组织-->文件夹和搜索选项   中设置“在同一窗口中打开每个文件夹” 如果设置后不起作用还可以 管理员方式执行以下两条命令 在开始菜单-运行中输入regsvr32 "%Sy ...

  10. ios9 3dtouch 博客

    http://my.oschina.net/u/2340880/blog/511509#OSC_h3_3