python使用杂谈笔记

本文介绍了Python中的一些实用技巧,包括跨版本兼容的模块导入、pip3的更新方法、Redis的操作方式、装饰器的使用示例、空值处理的简便方法以及Ubuntu下Python3.6.5的安装教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. import as配合try except

try:
    from greenlet import getcurrent as get_ident
except ImportError:
    try:
        from thread import get_ident
    except ImportError:
        from _thread import get_ident

参见flask的源码。按这种方式,我们在后面统一使用get_ident方法。而实际上这个get_iden方法根本就不是一个实现

 

二.如何在即有python27和python3的环境下,更新pip3到18.0

python3 -m pip install --upgrade pip --force-reinstall

 

三.python3对redis的使用操作

1.redis包安装 pip3 install redis

2.加载

 

四.对装饰器的理解

def canshu(id):
    print ("canshu=",id)
    def decorator(f,*args,**kwargs):
        print("decorator=",id)
        def addF(*args,**kwargs):
            print("(add F")
            f(*args,**kwargs)
        return addF
    return decorator

@canshu("1")
def testA(a,b):
    print (a+b)

@canshu("2")
def testB(a,b,c):
    print(a+b+c)
if __name__ == '__main__':
    # app.run()
    testA(1,2)
    testA(1,3)
    print("============================")
    testB(1,2,3)

五.如何便利一个对象又不用单独一行判空

for rulefactory in rules or ():
    self.add(rulefactory)

六.ubuntu如何安装python3.6.5https://blog.youkuaiyun.com/JiekeXu/article/details/80294523

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值