ITeye博客

Python

共 28 篇文章

Python偏函数

原创转载请注明出处: http://agilestyle.iteye.com/blog/2330401 当函数的参数个数太多,需要简化时,使用functools.partial可以创建一个新的函数,这个新函数可以固定住原函数的部分参数,从而在调用时更简单。 from functools import partial # int()函数可以把字符串转换为整数,当仅传入字符串时,int()函数默认按十进制转换 print(int('12345')) # int()函数还提供额外的base参数,默认值为10。如果传入base参数,就可以做N进制的转换 # 64 print(int('1000000…

2016-10-13 17:40 · Python

Python装饰器

原创转载请注明出处: http://agilestyle.iteye.com/blog/2330385 通过__name__属性获取函数的名字 def hello_world(): print('Hello World') f = hello_world # hello_world print(hello_world.__name__) # hello_world print(f.__name__) 接下来,我们为hello_world函数做一个增强,但是并不改变原函数的定义,而是在代码运行期间动态增加额外的功能,在Python中成为装饰器(Decorator),就好比Spring的AOP。…

2016-10-13 16:50 · Python

Python Closure

原创转载请注明出处: http://agilestyle.iteye.com/blog/2330379 先实现一个可变参数的求和—— 直接反回求和的结果 def calc_sum(*args): s = 0 for n in args: s = s + n return s # 15 print(calc_sum(1, 2, 3, 4, 5)) 改进一下,如果不需要立刻求和,后面根据需要再进行计算求和—— 返回求和的函数 def lazy_sum(*args): def total(): s = 0 for n in args: s = s + n return s return total…

2016-10-13 16:15 · Python

Python filter sorted

原创转载请注明出处: http://agilestyle.iteye.com/blog/2330331 Python内置的filter()函数用于过滤序列 过滤偶数 def is_odd(n): return n % 2 == 1 # [1, 3, 5, 7, 9] print(list(filter(is_odd, list(range(1, 10))))) 过滤空字符串 def not_empty(s): return s and s.strip() # ['a', 'b'] print(list(filter(not_empty, ['a', 'b', '', None]))) Con…

2016-10-13 13:49 · Python

Python map & reduce 以及lambda匿名函数

原创转载请注明出处: http://agilestyle.iteye.com/blog/2330300 map() map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。 使用map实现一个f(x) = x * x的功能 def f(x): return x * x m = map(f, list(range(1, 10))) # [1, 4, 9, 16, 25, 36, 49, 64, 81] print(list(m)) 另外可以使用lambda函数简化 # [1, 4, 9, 16, 25, …

2016-10-13 12:07 · Python

Python迭代器

原创装载请注明出处: http://agilestyle.iteye.com/blog/2329258 使用isinstance()判断一个对象是否是Iterable对象 # Iterable类型 from collections import Iterable # True print(isinstance([], Iterable)) # True print(isinstance((), Iterable)) # True print(isinstance({}, Iterable)) # True print(isinstance('abc', Iterable)) # True p…

2016-10-09 12:24 · Python

Python生成器

原创转载请注明出处: http://agilestyle.iteye.com/blog/2329232 将一个列表生成式的[]改为(),就创建了一个generator l = [x * x for x in range(1, 6)] print(l) g1 = (x * x for x in range(1, 6)) print(g1) for i in g1: print(i) Console Output generator保存的是算法,每次调用next(g),就计算出g的下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出StopIteration的错误 g2 = (x * …

2016-10-09 10:49 · Python

Python列表生成式

原创转载请注明出处: http://agilestyle.iteye.com/blog/2329193 # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(list(range(1, 11))) Console Output # [1x1, 2x2, 3x3, ..., 10x10] l1 = [x * x for x in range(1, 11)] print(l1) Console Output # [4, 16, 36, 64, 100] l2 = [x * x for x in range(1, 11) if x % 2 == 0] print(l2) …

2016-10-09 09:58 · Python

Python Iterable

原创转载请注明出处: http://agilestyle.iteye.com/blog/2328921 迭代Dict d = {'a': 1, 'b': 2, 'c': 3} # 默认情况下,dict迭代的是key for key in d: print(key) # 要迭代value,可以用for value in d.values() for value in d.values(): print(value) # 要同时迭代key和value,可以用for k, v in d.items() for k, v in d.items(): print(k, ":", v) Console …

2016-10-08 13:16 · Python

Python slice

原创转载请注明出处: http://agilestyle.iteye.com/blog/2328911 L = ['GuanYu', 'ZhangFei', 'ZhaoYun', 'MaChao', 'HuangZhong'] # get the first 3 elements # ['GuanYu', 'ZhangFei', 'ZhaoYun'] print(L[0:3]) # ['GuanYu', 'ZhangFei', 'ZhaoYun'] print(L[:3]) # get the last 2 elements # ['MaChao', 'HuangZhong'] print(…

2016-10-08 12:08 · Python

Python function parameter

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327771 位置参数 # 位置参数 def power(x): return x * x print(power(5)) Note: 对于power(x)函数,参数x就是一个位置参数。 默认参数 # 默认参数 # 设置默认参数时,有几点要注意: # 1.必选参数在前,默认参数在后,否则Python的解释器会报错 # 2.当函数有多个参数时,把变化大的参数放前面,变化小的参数放后面。变化小的参数就可以作为默认参数。 def power(x, n=2): s = 1 while n > 0: n = n …

2016-09-29 11:01 · Python

Python function

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327737 调用函数 # abs() print(abs(100)) print(abs(-20)) print(abs(12.34)) # max() print(max(1, 2)) print(max(2, 3, 1, -5)) # int() print(int('123')) print(int(12.34)) # float() print(float('12.34')) # str() print(str(1.23)) print(str(100)) print(isinstance(s…

2016-09-29 09:30 · Python

Python set

原创装载请注明出处: http://agilestyle.iteye.com/blog/2327559 set.py l = [1, 3, 2, 1, 2, 3] print(l) s = set(l) print(s) # add(key)方法可以添加元素到set中,可以重复添加,但不会有效果 s.add(4) print(s) # remove(key)方法可以删除元素 s.remove(4) print(s) s1 = set([1, 2, 3]) s2 = set([2, 3, 4]) # {2, 3} print(s1 & s2) # {1, 2, 3, 4} print(s1 |…

2016-09-29 08:55 · Python

Python dict

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327555 dict.py d = {'Wei': 'Caocao', 'Shu': 'Liubei', 'Wu': 'Sunquan'} print(d) print(d['Wei']) # 一个key只能对应一个value,所以,多次对一个key放入value,后面的值会把前面的值冲掉 d['Wei'] = 'Caopi' print(d) print(d['Wei']) # 要删除一个key,用pop(key)方法,对应的value也会从dict中删除 d.pop('Wu') print(d) …

2016-09-29 08:53 · Python

Python for while break continue

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327550 loop.py databases = ['oracle', 'mysql', 'cassandra', 'mongodb', 'hbase'] for database in databases: print(database) total = 0 for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: total = total + i print(total) # range(0, 5) print(range(5)) # [0, 1, 2, 3, 4] …

2016-09-28 13:50 · Python

Python if elif else

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327544 ifelse.py age = input() print('Your age is: ', age) # input()返回的数据类型是str,str不能直接和整数比较,必须先把str转换成整数 # Python提供int()函数来完成这件事情 age = int(age) # if <条件判断1>: # <执行1> # elif <条件判断2>: # <执行2> # elif <条件判断3>: # <执行3> # else: # <执行4> if age >= 18: print('a…

2016-09-28 13:21 · Python

Python Tuple

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327496 tuple和list非常类似,也是一种有序的集合,但是tuple一旦初始化就不能修改。 tuple.py classmates = ('Michael', 'Kobe', 'Kevin') print(classmates) print(len(classmates)) emptyTuple = () print(emptyTuple) print(len(emptyTuple)) #因为括号()既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这…

2016-09-28 10:20 · Python

Python List

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327491 list是一种有序的集合,可以随时添加和删除其中的元素。 list.py classmates = ['Michael', 'Kobe', 'Kevin'] print(classmates) #['Michael', 'Kobe', 'Kevin'] print(len(classmates)) #Michael print(classmates[0]) #Kobe print(classmates[1]) #Kevin print(classmates[2]) #Kevin print…

2016-09-28 09:55 · Python

Python Hello World

原创转载请注明出处: http://agilestyle.iteye.com/blog/2327489 helloworld.py print('Hello World') print('I love you', 'Mr Big') print('1 + 2 = ', 1 + 2) name = input('Please enter your name: ') print(name) Console Output

2016-09-28 09:50 · Python

Python OOP Detail

data attribute 方法 调用超类构造函数 重写方法 interface #Super 定义一个method函数以及在子类中期待一个动作的delegate class Super: def method(self): print('in Super.method') #Default behavior def delegate(self): self.action() #Expected to be defined #Inheritor 没有提供任何新的变量名,因此会获得Super中定义的一切内容 class Inheritor(Super): #Inherit method ve…

2013-04-24 13:00 · Python