来试试Python的几个“小坑”

Python是一门容易上手的语言,在测试圈也是非常流行,虽然看似简单,但是不够熟悉的话,还是容易踩到一些坑。话不多说,直接上题目,代码都很简短,推荐先自己写下答案,再对比本文最下边的参考答案。期待给你一种学了假Python的感觉(斜眼笑)。

以下代码在Py3.5 + Win10 + Pycharm下运行通过

题目1
a = [1, 2, 3, 4, 5]
a[1:3].append(666)
print(a)

a = [1, 2, 3, 4, 5]
a[1:3] = [666]
print(a)
题目2
a = [1, 2, 3, 4]

def modify(target):
    target = [1, 1, 1, 1]

modify(a)
print(a)
题目3
a = [1, ["a"]]
b = list(a)
a[-1].append("b")
a.append(4)
b[-1].pop(0)

print(a)
print(b)
题目4
import time

def func(when=time.time()):
    print(when)

func()
time.sleep(1)
func()
题目5
a = [1, 2, 2, 3, 4, 5, 2]
for i in a:
    if i == 2:
        a.remove(i)
print(a)
题目6
a = 257
b = 257
print(a is b)

def func():
    c = 257
    print(a is c)
    
func()
题目7
name = "煎饼"

def func():
    print(name)
    name = "煎饼啊"
func()
题目8
funcs = [lambda: str(i) for i in range(5)]
print(",".join([func() for func in funcs]))

funcs = [lambda i=i: str(i) for i in range(5)]
print(",".join([func() for func in funcs]))

funcs = (lambda: str(i) for i in range(5))
print(",".join([func() for func in funcs]))
题目9
class A:
    @classmethod
    def a(cls):
        pass

print(A.a is A.a)
print(A.a == A.a)
题目10
class A:
    pass

print(A() is A())
print(A() == A())
print(id(A()) == id(A()))
题目11
a = [1, 8, 15]
g = (i for i in a if a.count(i) > 0)
a = [2, 8, 16]
print(list(g))
题目12
a = (i for i in [1, 2, 2, 3, 3, 3])
g = (i for i in a if i in a)
print(list(g))
题目13
a = 6
result = (a > 20 and "a > 20" or a > 10 and "a > 10" or a > 5 and "a > 5" or a > 1 and "a > 1")
print(result)
题目14
# 在同级目录下,建立两个文件
# b.py
from a import print_a
def print_b():
    print("b")

# a.py
from b import print_b
def print_a():
    print("a")
    print_b()
print_a()

# 运行a.py,会打印出什么?
题目15
# 在同级目录下,建立两个文件
# b.py
def print_b():
    from a import print_a
    print("b")

# a.py
def print_a():
    print("a")
    from b import print_b
    print_b()

print_a()

# 运行a.py,会打印出什么?
题目16
from collections import defaultdict

tree = lambda: defaultdict(tree)
root = tree()
root["a"]["b"]["c"] = "d"
print(root["a"])
题目17
a = [[0] * 5] * 5
a[1][1] = 666
print(a)
题目18
a = [1, 2, [3, 4, [5, 6], 7], 8, 9]

def hi(target):
    for i in target:
        if isinstance(i, list):
            hi(i)
        else:
            yield i

print(list(hi(a)))
题目19
class A:
    def _get_num(self):
        return 1
    num = property(_get_num)

class B(A):
    def _get_num(self):
        return 2

b = B()
print(b.num)
题目20
import time

print("准备编写装饰器")

def set_log(func):
    print("装饰器顶层代码")
    def wrap(*args, **kwargs):
        print("装饰器内层代码")
        return func(*args, **kwargs)
    print("准备返回wrap对象")
    return wrap

print("准备编写demo函数")

@set_log
def demo():
    print("正在运行demo函数")

if __name__ == '__main__':
    print("准备运行demo函数")
    demo()

参考答案

题目1
[1, 2, 3, 4, 5]
[1, 666, 4, 5]
题目2
[1, 2, 3, 4]
题目3
[1, ['b'], 4]
[1, ['b']]
题目4
1507712088.8348608
1507712088.8348608
题目5
[1, 3, 4, 5, 2]
题目6
True
False
题目7
UnboundLocalError: local variable 'name' referenced before assignment
题目8
4,4,4,4,4
0,1,2,3,4
0,1,2,3,4
题目9
False
True
题目10
False
False
True
题目11
[8]
题目12
[]
题目13
a > 5
题目14
ImportError: cannot import name 'print_b'
题目15
a
a
b
b
题目16
defaultdict( at 0x00000232D1743E18>, {'b': defaultdict( at 0x00000232D1743E18>, {'c': 'd'})})
题目17
[[0, 666, 0, 0, 0], [0, 666, 0, 0, 0], [0, 666, 0, 0, 0], [0, 666, 0, 0, 0], [0, 666, 0, 0, 0]]
题目18
[1, 2, 8, 9]
题目19
1
题目20
准备编写装饰器
准备编写demo函数
装饰器顶层代码
准备返回wrap对象
准备运行demo函数
装饰器内层代码
正在运行demo函数

其实许多所谓的坑往往是对于Python运行的内部机制不太了解,这里推荐一个网站,它已可视化的形式展示Python代码的运行过程,点击前往 pythontutor

附个运行效果图

来试试Python的几个“小坑”_第1张图片

你可能感兴趣的:(来试试Python的几个“小坑”)