Python中TypeError: Unsupported data type: class NoneType和IndentationError: unexpected indent两个异常

运行出现如下报错
TypeError: Unsupported data type: <class 'NoneType'>

python2的编译报错如下:直接截图:
在这里插入图片描述
python3的编译报错如下:

D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py
Traceback (most recent call last):
  File "D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py", line 10, in <module>
    ips = IP(ip_s)
  File "D:\Python\Python36\lib\site-packages\IPy.py", line 262, in __init__
    raise TypeError("Unsupported data type: %s" % type(data))
TypeError: Unsupported data type: <class 'NoneType'>

Process finished with exit code 1

完整的报错信息:

D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py
Traceback (most recent call last):
  File "D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py", line 10, in <module>
    ips = IP(ip_s)
  File "D:\Python\Python36\lib\site-packages\IPy.py", line 262, in __init__
    raise TypeError("Unsupported data type: %s" % type(data))
TypeError: Unsupported data type: <class 'NoneType'>

Process finished with exit code 1


我的源代码:

#!/usr/bin/env python
from IPy import IP


def raw_input(param):
    pass


ip_s = raw_input('Please input an IP or net-range:')
ips = IP(ip_s)
if len(ips) > 1:
    print('net: %s' % ips.net())
    print('netmask: %s' % ips.netmask())
    print('broadcast: %s' % ips.broadcast())
    print('reverse address: %s' % ips.reverseNames()[0])
    print('subnet: %s' % len(ips))
else:
    print('reverse address: %s' % ips.reverseName()[0])

print('hexadecimal: %s' % ips.strHex())
print('binary ip: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())
print('ip_version: %s' % ips.version())

第一个报错

刚开始报错。第一次出现得到原因是因为缩进问题,在vim环境下,我设置了一个tab按键占4个字符,不知道为何还是报错:

IndentationError: unexpected indent
[root@devops python]# vim pyIPy.py 
[root@devops python]# python pyIPy.py 
  File "pyIPy.py", line 13
    print('subnet: %s' % len(ips))
    ^
IndentationError: unexpected indent

在这里插入图片描述
后来手动空格才不会出现缩进异常,

IndentationError: unexpected indent

问题就解决了

那么解决最初的异常

TypeError: Unsupported data type: <class 'NoneType'>

在python2的环境下进行代码修改:
在这里插入图片描述

#!/usr/bin/env python
from IPy import IP

# def raw_input(param):
#     pass
#
# ip_s = raw_input('Please input an IP or net-range:')
# ips = IP(ip_s)
ips = IP('192.168.217.0/24')
if len(ips) > 1:
    print('net: %s' % ips.net())
    print('netmask: %s' % ips.netmask())
    print('broadcast: %s' % ips.broadcast())
    print('reverse address: %s' % ips.reverseNames()[0])
    print('subnet: %s' % len(ips))
else:
    print('reverse address: %s' % ips.reverseName()[0])

print('hexadecimal: %s' % ips.strHex())
print('binary ip: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())
print('ip_version: %s' % ips.version())

输出结果:

D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py
net: 192.168.217.0
netmask: 255.255.255.0
broadcast: 192.168.217.255
reverse address: 217.168.192.in-addr.arpa.
subnet: 256
hexadecimal: 0xc0a8d900
binary ip: 11000000101010001101100100000000
iptype: PRIVATE
ip_version: 4

Process finished with exit code 0

结合完整的报错信息又加上刚刚的代码修改可以发现主要问题是出现在第8行代码

先根据报错测试一下:

#!/usr/bin/env python
from IPy import IP

def raw_input(param):
    pass

ip_s = raw_input('Please input an IP or net-range:')
#判断方法
if ip_s is None:    
    print("ip_s is NoneType")


输出结果:

ip_s is NoneType



那么就说明我们的raw_input()获取的是一个不存在的参数
Nonetype和空值是不一致的,可以理解为Nonetype为不存在这个参数。
空值表示参数存在,但是值为空。

这里顺便加入一点有关常用的判断测试:

type() 方法的语法:

type(name, bases, dict),其中name是类的名称,bases是基类的元组,dict是类内定义的命名空间变量。

当有一个参数时它的返回值是对象类型,。有三个参数时,返回值是新的类型对象。


isinstance() 方法的语法:

isinstance(object, classinfo),其中object 是实例对象,
变量,classinfo 可以是直接或间接类名、基本类型或者由它们组成的元组(如tuple,dict,int,str,float,list,set,bool,class类等)。
如果对象的类型与classinfo相同则返回 值为True,否则返回值为 Falseisinstance()type() 区别:

type() 不会认为子类是一种父类类型,不考虑继承关系。

isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同,推荐使用 isinstance()。一般type(x)用来看变量x的类型较多。

修改后的:
在这里插入图片描述

#!/usr/bin/env python
from IPy import IP

# def input(param):
#     pass

# ip_s = raw_input('Please input an IP or net-range:')
ip_s = input('Please input an IP or net-range:')
#判断
if ip_s is None:
    print("ip_s is NoneType")
    
ips = IP(ip_s)
if len(ips) > 1:
    print('net: %s' % ips.net())
    print('netmask: %s' % ips.netmask())
    print('broadcast: %s' % ips.broadcast())
    print('reverse address: %s' % ips.reverseNames()[0])
    print('subnet: %s' % len(ips))
else:
    print('reverse address: %s' % ips.reverseName()[0])

print('hexadecimal: %s' % ips.strHex())
print('binary ip: %s' % ips.strBin())
print('iptype: %s' % ips.iptype())
print('ip_version: %s' % ips.version())




输出结果:
D:\Python\Python36\python.exe D:/PycharmProjects/facetest1/fileface/psutilDemo/totalDemo.py
Please input an IP or net-range:192.168.217.135
reverse address: 1
hexadecimal: 0xc0a8d987
binary ip: 11000000101010001101100110000111
iptype: PRIVATE
ip_version: 4

Process finished with exit code 0

其实就是将raw_input() 更改为input()了

有关raw_input()
input() 的用法区别:
参考:

https://blog.csdn.net/qq_28513801/article/details/94843989


版权声明:本文为qq_28513801原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。