>>> 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 xiaoming
34 动态删除属性
删除对象的属性
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student('001','xiaoming')>>> delattr(xiaoming,'id')>>> hasattr(xiaoming,'id')False
35 动态获取对象属性
获取对象的属性
>>> 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')False
37 isinstance
判断object是否为classinfo的实例,是返回true
>>> class Student():def __init__(self,id,name):self.id = idself.name = name>>> xiaoming = Student('001','xiaoming')>>> isinstance(xiaoming,Student)True
38 父子关系鉴定
>>> 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))True
39 所有对象之根
object 是所有类的基类
>>> isinstance(1,object)True>>> isinstance([],object)True
40 一键查看对象所有方法
不带参数时返回当前范围内的变量、方法和定义的类型列表;带参数时返回参数的属性,方法列表 。
>>> 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 c
42 创建迭代器
>>> 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 called13579
43 创建range迭代器
- range(stop)
- range(start, stop[,step])
>>> t = range(11)>>> t = range(0,11,2)>>> for e in t:print(e)0246810
44 反向>>> rev = reversed([1,4,2,3,1])>>> for i in rev:print(i)13241
45 打包聚合各个可迭代对象的迭代器:
>>> 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 1
46 过滤器函数通过 lambda 表达式设定过滤条件,保留 lambda 表达式为True的元素:
>>> fil = filter(lambda x: x>10,[1,11,2,45,7,6,13])>>> for e in fil:print(e)114513
47 链式比较>>> i = 3>>> 1 < i < 3False>>> 1 < i <=3True
48 链式操作>>> 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
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 用Python从头开始实现简单遗传算法
- Pyqtgraph是Python平台上一种功能强大的2D/3D绘图库
- 这个小众网站,竟包含有100种实用工具,还能手把手教你修电脑
- Python的10个神奇的技巧
- 程序员用Python实现自动化控制键盘和鼠标
- 退休|49岁投了1000份简历没有找到工作,怎样才能工作到65岁?
- Python语法之Django框架-安装
- 一篇文章搞定Python连接mysql数据库模块PyMySQL
- 一篇文章搞懂Python协程
- 十几行Python代码轻松实现txt转xls,方便快捷