Interview

1.列表去重,保持顺序
1
2
a = [1, 3, 3, 60, 43, 630, 2, 1, 5]
b = sorted(list(set(a)), key=a.index)
2. 列表中带有字典,按照字典的值排序
1
2
a = [{'u': 2}, {'u': 1}, {'u': 3}]
b = sorted(a, key=(lambda x: x['u']))
3. 单例模式
1
2
3
4
5
6
7
8
9
class Singleton(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
orig = super(Singleton, cls)
cls._instance = orig.__new__(cls, *args, **kwargs)
return cls._instance

class MyClass(Singleton):
a = 1
1
2
3
4
5
6
7
8
9
10
11
def singleton(cls):
instances = {}
def getinstance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return getinstance

@singleton
class MyClass:
...
4. 什么情况下会出现StackOverflow
  • 递归过深可能会导致StackOverflow,比如设置sys.setrecursionlimit(1),可以导致这个结果出现

什么是Stack, Heap

闭包

装饰器,什么样的语言可以实现装饰器

__init__和__new__的区别

  • __new__是类方法, __init__是实例方法
  • __new__会返回一个创建的实例,__init__不返回任何东西
  • 由__new__创建cls的实例后,__init__才能被调用

HTTP, TCP/IP 分别在四层模型和七层模型的第几层,TCP/UDP的区别

网络协议

HTTP状态码的具体含义

  • 202 Accepted, 401 Unauthorized, 403 Forbidden, 405 Method Not Allowed, 502 Bad Gateway, 504 Gateway Timeout

  • 401 Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. 401 semantically means “unauthenticated”. i.e. the user does not have the necessary credentials.

  • 403 The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account of some sort.

HTTP Header里有哪些信息

  • 通用首部 Connection, Date, MIME-Version, Trailer, Transfer-Encoding, Update, Via, Accept, Authorization, Cookie
  • 请求首部 From, Host, Referer, User-Agent
  • 响应首部 Age, Public, Warning, Server
  • 实体首部 Allow, Content-Length, Content-Type, Last-Modified

cookie和session的区别

  • cookie存在客户端,session在服务端,session需要cookie来实现

Python2和Python3中的字符串有什么区别

1
2
3
4
5
6
# python2
type('abc') # <type 'str'>
type('abc'.decode()) # <type 'unicode'>
# python3
type('abc') # <class 'str'>
type('abc'.encode()) # <class 'bytes'>

async/await模型

Python的多线程,多进程,GIL分别使用的场景

进程通信

Python的传值方式

  • 可变对象是引用传递,不可变对象是值传递

Iteration和generator的区别

Python垃圾回收机制,什么时候执行

已经有了GIL, thread中为什么会有lock

SQL关键词的顺序,事务的几种状态