Python基础
1.说一说你所知道的 Python 数据类型有哪些。
2.Python 中列表和元组的区别是什么?元组是不是真的不可变?
3.什么是生成器和迭代器?它们之间有什么区别?
4.什么是闭包?装饰器又是什么?装饰器有什么作用?你用过装饰器吗?请写一个装饰器的例子。
5.说一下什么是匿名函数,用匿名函数有什么好处?
6.在学习 Python 的过程中,你有想过如何提高 Python 的运行效率吗?
7.用过类吗?知道继承吗?请写一个例子,用到继承。
8.说一下深拷贝和浅拷贝。
9.说一说什么是 TCP/IP 协议?
10.知道什么是三次握手和四次挥手吗?简单描述一下。
Post 和 Get 有什么区别?
TCP 和 UDP 有什么区别?
13.知道 Socket 网络编程吗?知道怎么用吗?
14. new 和 init的区别?
python中__new__和__init__的区别
__new__方法和__init__方法都是python中的构造方法,其中__new__方法使用较少,__init__方法使用较多。
首先来了解下这两种方法:
1.__new__是在实例创建之前被调用的,用于创建实例,然后返回该实例对象,是个静态方法。
2.__init__是当实例对象创建完成后被调用的,用于初始化一个类实例,是个实例方法。
__new__先被调用,__new__的返回值将传递给__init__方法的第一个参数,然后__init__被调用,给这个实例设置一些参数。
实例:
class A():
def __new__(cls, *args, **kwargs):
print('this is A __new__')
# return super(A, cls).__new__(cls) # 或者下面这种形式,两种都可
return object.__new__(cls)
def __init__(self, title):
print('this is A __init__')
self.title = title
a = A('python book')
输出结果:
this is A __new__
this is A __init__
两种方法的区别:
1.new至少要有一个参数cls,且必须要有返回值,返回的是实例化出来的实例,有两种return方式:
return super(父类,cls).__new__(cls)
或者
return object.__new__(cls)
2.init有一个参数self,就是这个new返回的实例,init在new基础上完成一些其他初始化的动作,init不需要有返回值;
注意事项:
1.如果new没有返回cls(即当前类)的实例,那么当前类的init方法是不会被调用的;
实例:
class A():
def __new__(cls, *args, **kwargs):
print('this is A __new__')
# return super(A, cls).__new__(cls)
def __init__(self, title):
print('this is A __init__')
self.title = title
a = A('python book')
输出结果:
this is A __new__
2.在定义子类的时候,没有重新定义new方法,那么直接继承父类的new方法构造该类的实例;
实例:
class A():
def __new__(cls, *args, **kwargs):
print('this is A __new__')
return super(A, cls).__new__(cls)
def __init__(self, title):
print('this is A __init__')
self.title = title
class B(A):
def __init__(self, title):
print('this is B __init__')
self.title = title
b = B('python book')
输出结果:
this is A __new__
this is B __init__
3.在定义子类的时候,重写new方法,就不会调用父类的new方法;
实例:
class A():
def __new__(cls, *args, **kwargs):
print('this is A __new__')
return super(A, cls).__new__(cls)
def __init__(self, title):
print('this is A __init__')
self.title = title
class B(A):
def __new__(cls, *args, **kwargs):
print('this is B __new__')
return super(A, cls).__new__(cls) # 或者下面return的方式
# return object.__new__(cls)
def __init__(self, title):
print('this is B __init__')
self.title = title
b = B('python book')
输出结果:
this is B __new__
this is B __init__
4.如果子类重写new方法时,return super(子类,cls),那么会调用父类的new方法构造类的实例;
实例:
class A():
def __new__(cls, *args, **kwargs):
print('this is A __new__')
return super(A, cls).__new__(cls)
def __init__(self, title):
print('this is A __init__')
self.title = title
class B(A):
def __new__(cls, *args, **kwargs):
print('this is B __new__')
return super(B, cls).__new__(cls)
def __init__(self, title):
print('this is B __init__')
self.title = title
b = B('python book')
输出结果:
this is B __new__
this is A __new__
this is B __init__