python小技巧

来源:互联网 发布:破解 经淘宝排查认定 编辑:程序博客网 时间:2024/05/28 09:34
# -*- coding: utf8 -*-# 三元运算符x = 0y = 10cond = Truez = x if cond else yprint z# python 2.5加入# for-in-else# 即在for 循环中,如果没有从任何一个break中退出,则会执行和for对应的else# 只要从break中退出了,则else部分不执行。for i in range(10):if i > 10:breakelse:print 'There is no number bigger than 10'# 打印一个对象def prn_obj(obj):      print ', '.join(['%s:%s' % item for item in obj.__dict__.items()])def test():m = 1def f1():m = 2def f2():print(m)f1()f2()test()

0 0