一、hash 哈希值:常用在keys的保存上。

  哈希值是通过哈希算法(一种逻辑算法)得到的数值,是一个对象唯一的数值表现形式,不同的内容哈希值不同

  具有哈希值的对象,在生存周期内不可变。列表、字典、元组、集合在生存周期内,内容改变时id不变,没有哈希值。

  相同数值,数字类型不同时,哈希值相同。

print(hash('name')) # -657320667255486260
print(hash(2))       # 2
print(hash(2.0))     # 2
print(hash(0o2))     # 2
print(hash('2'))     # 7518809203933579098
View Code

 二、 类和对象相关

  (一) isinstance  判断某个对象是否由某个类创建的

  与type()相似,但是在有subclass的时候,type判断会出现错误,所以尽量使用isinstance()判断所属类。

a = 12
s = 'hi'
class  A:
    pass
class  B(A):  # B是A的子类
    pass

# 判断对象是否由某一类创建的
print(isinstance(a,int)) # True
print(isinstance(s,int)) # False
print(isinstance(a,str)) # False
print(isinstance(B(),A)) # True
print(isinstance(A(),B)) # False
# 与type()的区别
print(isinstance(A(),A))   # True
print(isinstance(B(),A))   # True
print(type(A())==A)        # True
print(type(B())==A)        # False
#判断是否是其子类
print(issubclass(B,A))   # True
print(issubclass(A,B))   # False
View Code

  (二) issubclass  判断对象类是否是某个类的子类

  注意:A()表示类对象,A表示类。

  (三) object  所有类的基类

   如果一个类的创建,没有指明继承自哪一个类,默认继承object类,其中定义了一些类的公共方法。

class  A:
    pass
print(isinstance(A(),object))  # True
print(isinstance(str,object))  # True
View Code

  (四) property  类属性

   propert常用作装饰器。其作用是用来包装类的属性,这个属性可以根据实际需要,控制是否可读(设置fget参数)、可写(设置fset参数)、可删除(设置fdel参数)。

 三、 迭代器相关

  (一) iter  创建一个可迭代对象

  (二) next  取迭代中的一个值,下一个值,没有时报错。

  (三) yield  生成器

 四、sortd 与 reverse 排序

  (一) reverse  左右互换排放

  把列表中的元素左右调换排放,不对列表进行排序整理,原列表直接修改,不生成新列表

x = [1,4,3,6,5]
r = x.reverse()
print(x)  # [5, 6, 3, 4, 1]
print(r)  # None
View Code

  (二) sorted  指定排序

  按指定的方法对列表排序,与list.sort不同在于:生成新列表,原列表不变。

x = [1,4,3,6,5]
r = x.sort()
print(x)  # [1, 3, 4, 5, 6]
print(r)    # None
x = [1,4,3,6,5]
r = sorted(x)
print(x)  # [1, 4, 3, 6, 5]
print(r)  # [1, 3, 4, 5, 6]
View Code

 五、 __import__  导入模块

  调用模块是使用的import……或者from……import……,实际系统内部调用的此函数完成模块导入功能。