
正文
Python Q&A
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
http://ilian.i-n-i.org/python-interview-question-and-answers/
http://www.geekinterview.com/Interview-Questions/Programming/Python
http://www.reddit.com/r/Python/comments/1knw7z/python_interview_questions
http://www.toptal.com/python/interview-questions
http://careerride.com/python-interview-questions.aspx
http://www.oschina.net/translate/top-10-mistakes-that-python-programmers-make
http://www.toptal.com/python#hiring-guide
- Singleton
1.
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict);
cls.instance = None
def __call__(cls, *arg, **kw):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*arg, **kw);
return cls.instance;
class MyClass1(Singleton):
__metaclass__ = Singleton;
2.
def signleton(cls):
instance = [];
def getinstance():
if cls not in instance:
instance[ cls ] = cls();
return instnace[ cls ]
- What will be the output of the code below?
list = ['a', 'b', 'c', 'd', 'e']
print list[10:]
>>>[]
- What will be the output of the code below in Python 2? Explain your answer.
def div1(x,y):
print "%s/%s = %s" % (x, y, x/y)
def div2(x,y):
print "%s//%s = %s" % (x, y, x//y)
div1(5,2)
div1(5.,2)
div2(5,2)
div2(5.,2.)
Also, how would the answer differ in Python 3 (assuming, of course, that the above
print
statements were converted to Python 3 syntax)?
5/2 = 2
5.0/2 = 2.5
5//2 = 2
5.0//2.0 = 2.0
-What will be the output of the code below? Explain your answer.
def extendList(val, list=[]):
list.append(val)
return list
list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')
print "list1 = %s" % list1
print "list2 = %s" % list2
print "list3 = %s" % list3
How would you modify the definition of
extendList
to produce the presumably desired behavior?
-What will be the output of the code below? Explain your answer.
class Parent(object):
x = 1
class Child1(Parent):
pass
class Child2(Parent):
pass
print Parent.x, Child1.x, Child2.x
Child1.x = 2
print Parent.x, Child1.x, Child2.x
Parent.x = 3
print Parent.x, Child1.x, Child2.x
1 1 1
1 2 1
3 2 3
- What will be the output of the code below? Explain your answer.
def multipliers():
return [lambda x : i * x for i in range(4)]
print [m(2) for m in multipliers()]
How would you modify the definition of
multipliers
to produce the presumably desired behavior?
[6, 6, 6, 6]
def multipliers():
return [lambda x, i=i : i * x for i in range(4)]
- 去掉 list 中的重复元素
def unique(L):
return list(set(L))
ll = [1, 3, 3, 4, 5, 5, 5]
print unique(ll)
循环的数据结构会导致循环
尽管这在实际情况中很少见,但是如果一个对象的集合包含了到它自己的引用,这被称为循环对象(cyclic object)。如果在一个对象中发现一个循环,Python会输出一个[…],以避免在无限循环中卡住:
>>> L
=
[
'grail'
]
# 在 L中又引用L自身会
>>> L.append(L)
# 在对象中创造一个循环
>>> L
[
'grail'
, [...]]
赋值语句不会创建对象的副本,仅仅创建引用
>>> L
=
[
1
,
2
,
3
]
# 共用的列表对象
>>> M
=
[
'X'
, L,
'Y'
]
# 嵌入一个到L的引用
>>> M
[
'X'
, [
1
,
2
,
3
],
'Y'
]
>>> L[
1
]
=
0
# 也改变了M
>>> M
[
'X'
, [
1
,
0
,
3
],
'Y'
]
>>> L
=
[
1
,
2
,
3
]
>>> M
=
[
'X'
, L[:],
'Y'
]
# 嵌入一个L的副本
>>> L[
1
]
=
0
# 仅仅改变了L,但是不影响M
>>> L
[
1
,
0
,
3
]
>>> M
[
'X'
, [
1
,
2
,
3
],
'Y'
]
默认参数和可变对象
在执行def语句时,默认参数的值只被解析并保存一次,而不是每次在调用函数的时候。这通常是你想要的那样,但是因为默认值需要在每次调用时都保持同样对象,
在执行def语句时,默认参数的值只被解析并保存一次,而不是每次在调用函数的时候。这通常是你想要的那样,但是因为默认值需要在每次调用时都保持同样对象,
你在试图改变可变的默认值(mutable defaults)的时候可要小心了。例如,下面的函数中使用一个空的列表作为默认值,然后在之后每一次函数调用的时候改变它的值:
>>> def saver(x=[]): # 保存一个列表对象
... x.append(1) # 并每次调用的时候
... print x # 改变它的值
...
>>> saver([2]) # 未使用默认值
[2, 1]
>>> saver() # 使用默认值
[1]
>>> saver() # 每次调用都会增加!
[1, 1]
>>> saver()
[1, 1, 1]
>>> def saver(x=[]): # 保存一个列表对象
... x.append(1) # 并每次调用的时候
... print x # 改变它的值
...
>>> saver([2]) # 未使用默认值
[2, 1]
>>> saver() # 使用默认值
[1]
>>> saver() # 每次调用都会增加!
[1, 1]
>>> saver()
[1, 1, 1]
有的人将这个视作Python的一个特点——因为可变的默认参数在每次函数调用时保持了它们的状态,它们能提供像C语言中静态本地函数变量的类似的一些功能。但是,当你第一次碰到它时会觉得这很奇怪,
并且在Python中有更加简单的办法来在不同的调用之间保存状态(比如说类)。
要摆脱这样的行为,在函数开始的地方用切片或者方法来创建默认参数的副本,或者将默认值的表达式移到函数里面;只要每次函数调用时这些值在函数里,就会每次都得到一个新的对象:
>>> def saver(x=None):
... if x is None: x = [] # 没有传入参数?
... x.append(1) # 改变新的列表
... print x
...
>>> saver([2]) # 没有使用默认值
[2, 1]
>>> saver() # 这次不会变了
[1]
>>> saver()
[1]
常见错误 #3: 为 except 指定错误的参数
假设你有如下一段代码:
>>>
try
:
... l
=
[
"a"
,
"b"
]
...
int
(l[
2
])
...
except
ValueError, IndexError:
# To catch both exceptions, right?
...
pass
Traceback (most recent call last):
File
"<stdin>"
, line
3
,
in
<module>
IndexError:
list
index out of
range
这里的问题在于 except 语句并不接受以这种方式指定的异常列表。相反,在Python 2.x中,使用语法 except Exception, e 是将一个异常对象绑定到第二个 可选 参数(在这个例子中是 e)上,以便在后面使用。
所以,在上面这个例子中,IndexError 这个异常并 不 是 被except语句捕捉到的,而是被绑定到一个名叫 IndexError的参数上时引发的。
在一个except语句中捕获多个异常的正确做法是将第一个参数指定为一个含有所有要捕获异常的元组。并且,为了代码的可移植性,要使用as关键词,因为Python 2 和Python 3都支持这种语法:
>>>
try
:
... l
=
[
"a"
,
"b"
]
...
int
(l[
2
])
...
except
(ValueError, IndexError) as e:
...
pass
>>>
-
>>> lst = [1, 2, 3]
>>> def foo1():
... lst.append(5) # 没有问题...
...
>>> foo1()
>>> lst
[1, 2, 3, 5]
>>> lst = [1, 2, 3]
>>> def foo2():
... lst += [5] # ... 但是这里有问题!
...
>>> foo2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'lst' referenced before assignment
嗯?为什么 foo2 报错,而foo1没有问题呢?
原因和之前那个例子的一样,不过更加令人难以捉摸。foo1 没有对 lst 进行赋值操作,而 foo2 做了。要知道, lst += [5] 是 lst = lst + [5] 的缩写,我们试图对 lst 进行赋值操作(Python把他当成了局部变量)。
>>> def foo1():
... lst.append(5) # 没有问题...
...
>>> foo1()
>>> lst
[1, 2, 3, 5]
>>> lst = [1, 2, 3]
>>> def foo2():
... lst += [5] # ... 但是这里有问题!
...
>>> foo2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'lst' referenced before assignment
嗯?为什么 foo2 报错,而foo1没有问题呢?
原因和之前那个例子的一样,不过更加令人难以捉摸。foo1 没有对 lst 进行赋值操作,而 foo2 做了。要知道, lst += [5] 是 lst = lst + [5] 的缩写,我们试图对 lst 进行赋值操作(Python把他当成了局部变量)。
此外,我们对 lst 进行的赋值操作是基于 lst 自身(这再一次被Python当成了局部变量),但此时还未定义。因此出错!
>>> # INITIALIZING AN ARRAY -- METHOD 1
>>> x = [[1,2,3,4]] * 3
>>> x
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
>>> # INITIALIZING AN ARRAY -- METHOD 2
>>> y = [[1,2,3,4] for _ in range(3)]
>>> y
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
>>> x[0][3] = 99
>>> x
[[1, 2, 3, 99], [1, 2, 3, 99], [1, 2, 3, 99]]
>>> y[0][3] = 99
>>> y
[[1, 2, 3, 99], [1, 2, 3, 4], [1, 2, 3, 4]]
- 简单地交换2个元素
>>> x,y = y,x
Q: What will be printed out by the last statement below?
>>> flist = []
>>> for i in range(3):
... flist.append(lambda: i)
...
>>> [f() for f in flist] # what will this print out?
In any closure in Python, variables are bound by name. Thus, the above line of code will print out the following:
[2, 2, 2]
Presumably not what the author of the above code intended!
A workaround is to either create a separate function or to pass the args by name; e.g.:
>>> flist = []
>>> for i in range(3):
... flist.append(lambda i = i : i)
...
>>> [f() for f in flist]
[0, 1, 2]
Python Q&A的更多相关文章
-
Python下载网页的几种方法
get和post方式总结 get方式:以URL字串本身传递数据参数,在服务器端可以从'QUERY_STRING'这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据(只能是字符串,比如在 ...
-
Python自动化运维之16、线程、进程、协程、queue队列
一.线程 1.什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位. 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行 ...
-
关于Python编程的一些问答
关于Python编程的一些问答 导语 大约1个月前,oschina.net和华章图书一起合作做了一个活动:OSC第51期高手问答--聊聊python那些事,来推广我参与撰写的书<编写高质量代码: ...
-
python进程池
当需要创建的子进程数量不多时,可以直接利用multiprocessing中的Process动态成生多个进程,但如果是上百甚至上千个目标,手动的去创建进程的工作量巨大,此时就可以用到multiproce ...
-
python 中的queue, deque
python3 deque(双向队列) 创建双向队列 import collections d = collections.deque() append(往右边添加一个元素) import colle ...
-
python中*和**的打包和解包
python中的*和**,能够让函数支持任意数量的参数,它们在函数定义和调用中,有着不同的目的 一. 打包参数 * 的作用:在函数定义中,收集所有的位置参数到一个新的元组,并将这个元组赋值给变量arg ...
-
Python多线程-队列
队列就是存东西取东西,多用于多线程中 按照顺序 对传入的数据按规定的顺序输出 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" ...
-
python interview questions
referce:python interview questions top 50 refercence:python interview questions top 15 summary Q: wh ...
-
Python扫描器-端口扫描
结合渗透测试最常见就是单个域名扫指纹,自动子域名查找.获取所有子域名的IP,自动C段IP查找相同子域名,利用有效IP扫端口. 常见端口库扫描 service_list = { 21:"FTP ...
随机推荐
-
Back to openGL!
#include <iostream> #include <windows.h> #include <gl/glut.h> #include <math.h& ...
-
DotNetBar TabControl的使用
这个和系统的TabPage不同,一个TabPage分为了DevComponents.DotNetBar.TabItem,DevComponents.DotNetBar.TabControlPanel两 ...
-
Thinkphp_基础(2)URL模式
URL请求 ThinkPHP采用单一入口模式访问应用,对应用的所有请求都定向到应用的入口文件,系统会从URL参数中解析当前请求的模块.控制器和操作,下面是一个标准的URL访问格式: http://se ...
-
C语言基础回顾
第一章 C语言基础 1. C语言编译过程 预处理:宏替换.条件编译.头文件包含.特殊符号 编译.优化:翻译并优化成等价的中间代码表示或汇编代码 汇编:生成目标文件,及与源程序等效的目标的机器语言代码 ...
-
eclipse建包出现多层包
-
jQuery HTML
alert("Text: " + $("#test").text());获取text alert("HTML: " + $("#t ...
-
datatables增删改查的实现
学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...
-
iOS开发~CocoaPods使用详细说明
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...
-
bootstrap-14
基础导航条: 使用方法:1. 首先在制作导航的列表(<ul class="nav">)基础上添加类名"navbar-nav" 2.在列表外部添加一个 ...
-
Java socket长连接代码实现
服务器端程序: import java.io.*; import java.net.*; import java.util.*; public class ChatServer { boolean s ...
get和post方式总结 get方式:以URL字串本身传递数据参数,在服务器端可以从'QUERY_STRING'这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据(只能是字符串,比如在 ...
一.线程 1.什么是线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位. 一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行 ...
关于Python编程的一些问答 导语 大约1个月前,oschina.net和华章图书一起合作做了一个活动:OSC第51期高手问答--聊聊python那些事,来推广我参与撰写的书<编写高质量代码: ...
当需要创建的子进程数量不多时,可以直接利用multiprocessing中的Process动态成生多个进程,但如果是上百甚至上千个目标,手动的去创建进程的工作量巨大,此时就可以用到multiproce ...
python3 deque(双向队列) 创建双向队列 import collections d = collections.deque() append(往右边添加一个元素) import colle ...
python中的*和**,能够让函数支持任意数量的参数,它们在函数定义和调用中,有着不同的目的 一. 打包参数 * 的作用:在函数定义中,收集所有的位置参数到一个新的元组,并将这个元组赋值给变量arg ...
队列就是存东西取东西,多用于多线程中 按照顺序 对传入的数据按规定的顺序输出 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" ...
referce:python interview questions top 50 refercence:python interview questions top 15 summary Q: wh ...
结合渗透测试最常见就是单个域名扫指纹,自动子域名查找.获取所有子域名的IP,自动C段IP查找相同子域名,利用有效IP扫端口. 常见端口库扫描 service_list = { 21:"FTP ...
-
Back to openGL!
#include <iostream> #include <windows.h> #include <gl/glut.h> #include <math.h& ...
-
DotNetBar TabControl的使用
这个和系统的TabPage不同,一个TabPage分为了DevComponents.DotNetBar.TabItem,DevComponents.DotNetBar.TabControlPanel两 ...
-
Thinkphp_基础(2)URL模式
URL请求 ThinkPHP采用单一入口模式访问应用,对应用的所有请求都定向到应用的入口文件,系统会从URL参数中解析当前请求的模块.控制器和操作,下面是一个标准的URL访问格式: http://se ...
-
C语言基础回顾
第一章 C语言基础 1. C语言编译过程 预处理:宏替换.条件编译.头文件包含.特殊符号 编译.优化:翻译并优化成等价的中间代码表示或汇编代码 汇编:生成目标文件,及与源程序等效的目标的机器语言代码 ...
-
eclipse建包出现多层包
-
jQuery HTML
alert("Text: " + $("#test").text());获取text alert("HTML: " + $("#t ...
-
datatables增删改查的实现
学习可参考:http://www.guoxk.com/node/jquery-datatables http://yuemeiqing2008-163-com.iteye.com/blog/20069 ...
-
iOS开发~CocoaPods使用详细说明
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...
-
bootstrap-14
基础导航条: 使用方法:1. 首先在制作导航的列表(<ul class="nav">)基础上添加类名"navbar-nav" 2.在列表外部添加一个 ...
-
Java socket长连接代码实现
服务器端程序: import java.io.*; import java.net.*; import java.util.*; public class ChatServer { boolean s ...







