In Python, does `is not int` ever return false?
我正在使用参数解析器允许将端口号传递给我的程序。 然后我尝试验证该值,并且第一个测试之一是
1 2 3 4 5 6 7 8 9 10 11 | parser = argparse.ArgumentParser(description='Provides XML-RPC API.') parser.add_argument('--port', dest='port', default=9666, type=int, help='port to listen on, 9666 by default.'); args = parser.parse_args() if args.port is not int: raise TypeError("Port must be integer (%s given), use -h argument to get help."%(type(args.port).__name__)) if args.port>=65536 or args.port<=0: raise IndexError("Port must be lower than 65536, 65535 is the maximum port number. Also, port can only be positive, non-zero number. Use -h argument to get help.") if args.port<=1204: print("Notice: ports up to 1024 are reserved to well known services, using this port numbers often leads to conflicts. Use -h argument to get help.") |
无论我做什么,我都会收到错误:
1 | Port must be integer (int given), use -h argument to get help. |
我的意思是,到底是什么? 即使错误说它是int,所以发生了什么?
如果
1 2 | args.port = int args.port is int # True |
或者你将
1 2 | int = args.port args.port is int # True |
那是因为
也许您对如何测试对象类型感到困惑,因为您使用
1 2 | type(args.port) is not int # False if args.port is a different type not isinstance(args.port, int) # False if args.port is a different type and not a subclass either |
这也不会是这种情况,因为您将
1 2 | parser.add_argument('--port', dest='port', default=9666, type=int, help='port to listen on, 9666 by default.') |
1 2 3 4 5 6 7 8 | >>> import argparse >>> parser = argparse.ArgumentParser(description='Provides XML-RPC API.') >>> parser.add_argument('--port', dest='port', default=9666, type=int, ... help='port to listen on, 9666 by default.') _StoreAction(option_strings=['--port'], dest='port', nargs=None, const=None, default=9666, type=<type 'int'>, choices=None, help='port to listen on, 9666 by default.', metavar=None) >>> parser.parse_args(['--port', 'not an integer']) usage: [-h] [--port PORT] : error: argument --port: invalid int value: 'not an integer' |
那是因为
1 2 3 4 | >>> int('not an integer') Traceback (most recent call last): File"<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: 'not an integer' |
您可以提供限制范围的自己的会话功能:
1 2 3 4 5 | def portnumber(value): port = int(value) # may raise ValueError if not 0 <= port <= 65536: raise ValueError('Not a valid port number') return port |
然后将其用作
1 2 | parser.add_argument('--port', dest='port', default=9666, type=portnumber, help='port to listen on, 9666 by default.') |
通常,测试某些东西是
'int'指的是类本身。如果你自己传递了'int'类,那么'is not int'将评估为False,这种情况不太可能发生。你可能想要'isinstance(args.port,int)'。
除非您执行
让我们说你试试吧
1 | >>> 1 is not int |
这将返回
1 2 | >>> type(1) <class 'int'> |
正如您所看到的,
要解决这个问题,你可以这样做
1 | not isinstance(args.port, int) |
正如其他答案所建议的那样
你也可以这样做
1 | type(args.port) is not int |
检查