Python---class and iter
来源:互联网 发布:网络名词 编辑:程序博客网 时间:2024/06/05 07:53
import itertoolsimport subprocessimport util_m'''it is for shell command'''import builtins'''it is loaded into memory when python module starts'''class myclass1:pass'''pass means {} in java or c'''class myclass:def __init__(self, max):self.max=maxdef __next__(self):flib = self.aif flib > self.max:raise StopIterationself.a,self.b= self.b, self.a+self.breturn flibdef __iter__(self):self.a = 0self.b=1return self'''__init__(self) means contruction functionself means this in c++__iter__ is used when for loop start, and return interator of self__next__ is used when for loop next, when StopIteration happen , for loop quit''''''generator expression=====================, use () , it means generator iterator, not to use others'''gen1 = (ord(c) for c in {'E', 'B','S','W'})'''generator function=========================, return ord(c) every time, it is good use'''def getord(string1):for c in string1:yield ord(c)def per1():perm1=itertools.permutations([1,2,3],2)list1=list(perm1)for item in list1:yield item'''permutations return iteratorlist(iterator) returns list, list is many datause for loop to get each itembecause per1 is util app, it not good to print info here, so return generator objectin caller, we can use for loop to print generator object, no need to call next() because for loop is smart'''def group1():name=['candy', 'mandy','hebe']print(list(itertools.groupby(name, len)))'''return key, value_list'''def chain1():print(list(range(0,5)))print(list(itertools.chain(range(0,5), range(6,10))))print(list(zip(range(0,5), range(6,10))))'''match util 3, because 9 is the last in latter list'''print(list(itertools.zip_longest(range(0,5), range(6,10))))'''range(0,5)------0,1,2,3,4. match util 4'''def trans1():table1={ord('A'):ord('B')}print('Able'.translate(table1))def eval1():x=5eval('1+1')print(eval('x*5'))print(eval('"A"+"B"'))print(eval('__import__("subprocess").getoutput("ls ~/ -l")'))subprocess.call(['df','-h'])subprocess.call(['uname', '-a'])'''eval can calculate the expression in stringbut it is not safe, it will be attacked by hacker'''def subp1():print(subprocess.getoutput('ls ~/ -l'))def builthello():print('hello__builtins__')if __name__ == '__main__':c1 = myclass(6)print(c1.max)for n in c1:print(n)try:assert 1>2, 'a is not bigger than b'except AssertionError:print('I got exception')print(next(gen1))print(next(gen1))gen2=getord({'E','B','S','W'})print(next(gen2))print(next(gen2))for item in per1():print(item)for item2 in list(itertools.permutations([1,2,3], 2)):print(item2)print(list(itertools.product('ABC','123')))print(list(itertools.combinations('ABC',2)))group1()chain1()trans1()eval1()subp1()__builtins__.__dict__['hello']=builthello'''add hello api to __builtins__, to load this api when python module starts'''hello()util_m.built()'''for loop is smart, it can call next automatically'''
0 0
- Python---class and iter
- Python 中的 generator ,class 的iter
- Python iter
- python map iter
- python iter( )函数
- Python之iter
- python初识iter
- Python iter函数详解
- python iter()用法
- Python 迭代器(iter)
- python: iter、next函数
- python iter( )函数
- python object and class
- python Object And Class
- python object and class
- python中迭代器和iter()函数
- python迭代器和iter()函数
- python中迭代器和iter()函数
- Python---serial
- OC基础---初始化方法
- hdu HDU Today2112
- 成员变量、局部变量和全局变量
- [IOS]SQLite3常用语句
- Python---class and iter
- 【LVL1_6_c】 指针数组 数组指针 的区别(仅仅是初步学习理解)
- OC基础----查看苹果帮助文档
- Python---file
- JAVA 死锁例子
- 流程控制与数组
- Python---http
- MVC5+EasyUI企业开发框架系列(01)—总体介绍
- 正则表达式: 以英文字母开头,只能包含英文字母、数字、下划线