python 基础问题,包括语法等

在函数中使用全局变量

  先在函数中用global声明一下就可以了,不需要也不能(?)用一次声明一次

def model_choose(str,model):
    #jieba_cut=functools.partial(jieba.cut,cut_all=True)
    global result   #使用result
    if model==1:#标志着是全模式
        result.append('全模式')#不能将行代码包括下面的都写成global result...
        jieba.cut(str,cut_all=True)
    elif model==2:#标志着精确模式
        result.append('精确模式')
        return jieba.cut(str,cut_all=False)
    else:#搜索硬性模式
        result.append('搜索引擎模式')
        return jieba.cut_for_search(str)
str ='可以使用类似今天看那个偏函数'
model=2
result=[]

  

猜你喜欢

转载自www.cnblogs.com/Gaoqiking/p/10739502.html
今日推荐