How to convert Nonetype to int or string?

前端 未结 10 1985
情歌与酒
情歌与酒 2020-11-29 17:23

I\'ve got an Nonetype value x, it\'s generally a number, but could be None. I want to divide it by a number, but Python raises:

<
10条回答
  •  庸人自扰
    2020-11-29 18:04

    In some situations it is helpful to have a function to convert None to int zero:

    def nz(value):
    
        '''
        Convert None to int zero else return value.
        '''
    
        if value == None:
            return 0
        return value
    

提交回复
热议问题