什么是“NoneType"对象? [英] What is a 'NoneType' object?

查看:165
本文介绍了什么是“NoneType"对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 python 脚本时出现此错误:

I'm getting this error when I run my python script:

TypeError: cannot concatenate 'str' and 'NoneType' objects

我很确定str"表示字符串,但我不知道NoneType"对象是什么.我的脚本在第二行失败了,我知道第一个行得通,因为该行中的命令正如我所期望的那样在我的 asa 中.起初我以为可能是因为我在 send_command 中使用了变量和用户输入.

I'm pretty sure the 'str' means string, but I dont know what a 'NoneType' object is. My script craps out on the second line, I know the first one works because the commands from that line are in my asa as I would expect. At first I thought it may be because I'm using variables and user input inside send_command.

'CAPS' 中的所有内容都是变量,'小写' 中的所有内容都是从 'parser.add_option' 选项输入的.

Everything in 'CAPS' are variables, everything in 'lower case' is input from 'parser.add_option' options.

我正在使用 pexpect 和 optparse

I'm using pexpect, and optparse

send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)
send_command(child, SNMPSRVUSRCMD + snmpuser + group + V3AUTHCMD + snmphmac + snmpauth + PRIVCMD + snmpencrypt + snmppriv)

推荐答案

NoneTypeNone 对象的类型,该对象表示 no价值.None 是不返回任何东西"的函数的返回值.对于搜索某些东西并且可能会或可能不会找到它的函数来说,它也是一个常见的默认返回值;例如,当正则表达式不匹配时由 re.search 返回,当键在 dict 中没有条目时由 dict.get 返回.您不能将 None 添加到字符串或其他对象.

NoneType is the type for the None object, which is an object that indicates no value. None is the return value of functions that "don't return anything". It is also a common default return value for functions that search for something and may or may not find it; for example, it's returned by re.search when the regex doesn't match, or dict.get when the key has no entry in the dict. You cannot add None to strings or other objects.

您的变量之一是 None,而不是字符串.也许你忘记在你的一个函数中return,或者用户没有提供命令行选项而optparse给了你None对于该选项的值.当您尝试将 None 添加到字符串时,您会得到该异常:

One of your variables is None, not a string. Maybe you forgot to return in one of your functions, or maybe the user didn't provide a command-line option and optparse gave you None for that option's value. When you try to add None to a string, you get that exception:

send_command(child, SNMPGROUPCMD + group + V3PRIVCMD)

groupSNMPGROUPCMDV3PRIVCMD 之一的值为 None.

One of group or SNMPGROUPCMD or V3PRIVCMD has None as its value.

这篇关于什么是“NoneType"对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆