python中的三元运算
[on true] if [expression] else [on false]
如果表达式为True,就执行[on true]中的语句
否则,就执行[on false]中的语句
a,b=2,3
min=a if a<b else b
解释一下python中的继承
当一个类继承自另一个类,它就被成为一个子类/派生类,继承父类/基类。超类。
它会继承/获取所有类成员(属性和方法)
单继承:一个类继承自单个基类
多继承:一个类继承自多个基类
多级继承:一个类继承自单个基类,后者则继承自另一个基类
分层继承:多个类继承自单个基类
混合继承:两种或多种类型继承的混合
解释python中的help() 和 dir() 函数
Help()函数是一个内置函数,用于查看函数或模块用途的详细说明:
import copy
help(copy.copy)
Dir()函数也是Python内置函数,dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。
dir(copy.copy)
请解释使用args和*kwargs的含义
当我们不知道向函数传递多少参数时,比如我们向传递一个列表或元组,我们就使用*args。
>>> def func(*args):
for i in args:
print(i)
>>> func(3,2,1,4,7)
在我们不知道该传递多少关键字参数时,使用**kwargs来收集关键字参数。
>>> def func(**kwargs):
for i in kwargs:
print(i,kwargs[i])
>>> func(a=1,b=2,c=7)
什么是负索引?
#负索引和正索引不同,它是从右边开始检索。
#它也能用于列表中的切片:
mylist=[0,1,2,3,4,5,6,7,8]
mylist[-3]
mylist[-6:-1]