Python 100个样例代码( 三 )


>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student(id='1',name='xiaoming')>>> callable(xiaoming)False如果 xiaoming能被调用 , 需要重写Student类的call方法:
>>> class Student():def __init__(self,id,name):self.id = idself.name = name此时调用 xiaoming():
>>> xiaoming = Student('001','xiaoming')>>> xiaoming()I can be calledmy name is xiaoming34 动态删除属性
删除对象的属性
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student('001','xiaoming')>>> delattr(xiaoming,'id')>>> hasattr(xiaoming,'id')False35 动态获取对象属性
获取对象的属性
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student('001','xiaoming')>>> getattr(xiaoming,'name') *# 获取name的属性值*'xiaoming'36 对象是否有某个属性
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student('001','xiaoming')>>> getattr(xiaoming,'name')*# 判断 xiaoming有无 name属性*'xiaoming'>>> hasattr(xiaoming,'name')True>>> hasattr(xiaoming,'address')False37 isinstance
判断object是否为classinfo的实例,是返回true
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student('001','xiaoming')>>> isinstance(xiaoming,Student)True38 父子关系鉴定
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> class Undergraduate(Student):pass*# 判断 Undergraduate 类是否为 Student 的子类 *>>> issubclass(Undergraduate,Student)True第二个参数可为元组:
>>> issubclass(int,(int,float))True39 所有对象之根
object 是所有类的基类
>>> isinstance(1,object)True>>> isinstance([],object)True40 一键查看对象所有方法
不带参数时返回当前范围内的变量、方法和定义的类型列表;带参数时返回参数的属性,方法列表 。
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student('001','xiaoming')>>> dir(xiaoming)['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'id', 'name']41 枚举对象
Python 的枚举对象
>>> s = ["a","b","c"]>>> for i,v in enumerate(s):print(i,v)0 a1 b2 c42 创建迭代器
>>> class TestIter():def __init__(self,lst):self.lst = lst*# 重写可迭代协议__iter__*def __iter__(self):print('__iter__ is called')return iter(self.lst)迭代 TestIter 类:
>>> t = TestIter()>>> t = TestIter([1,3,5,7,9])>>> for e in t:print(e)__iter__ is called1357943 创建range迭代器

  1. range(stop)
  2. range(start, stop[,step])
生成一个不可变序列的迭代器:
>>> t = range(11)>>> t = range(0,11,2)>>> for e in t:print(e)024681044 反向
>>> rev = reversed([1,4,2,3,1])>>> for i in rev:print(i)1324145 打包
聚合各个可迭代对象的迭代器:
>>> x = [3,2,1]>>> y = [4,5,6]>>> list(zip(y,x))[(4, 3), (5, 2), (6, 1)]>>> for i,j in zip(y,x):print(i,j)4 35 26 146 过滤器
函数通过 lambda 表达式设定过滤条件,保留 lambda 表达式为True的元素:
>>> fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])>>> for e in fil:print(e)11451347 链式比较
>>> i = 3>>> 1 < i < 3False>>> 1 < i <=3True48 链式操作
>>> from operator import (add, sub)>>> def add_or_sub(a, b, oper):return (add if oper == '+' else sub)(a, b)>>> add_or_sub(1, 2, '-')-1


推荐阅读