python如果想测试变量的类型、可以使用_在Python中测试变量类型

我正在为类“Room”创建一个初始化函数,发现程序不会接受我对输入变量所做的测试。

这是为什么?def __init__(self, code, name, type, size, description, objects, exits):

self.code = code

self.name = name

self.type = type

self.size = size

self.description = description

self.objects = objects

self.exits = exits

#Check for input errors:

if type(self.code) != type(str()):

print 'Error found in module rooms.py!'

print 'Error number: 110'

elif type(self.name) != type(str()):

print 'Error found in module rooms.py!'

print 'Error number: 111'

elif type(self.type) != type(str()):

print 'Error found in module rooms.py!'

print 'Error number: 112'

elif type(self.size) != type(int()):

print 'Error found in module rooms.py!'

print 'Error number: 113'

elif type(self.description) != type(str()):

print 'Error found in module rooms.py!'

print 'Error number: 114'

elif type(self.objects) != type(list()):

print 'Error found in module rooms.py!'

print 'Error number: 115'

elif type(self.exits) != type(tuple()):

print 'Error found in module rooms.py!'

print 'Error number: 116'

运行此命令时,会出现以下错误:Traceback (most recent call last):

File "/Users/Jasper/Development/Programming/MyProjects/Game Making Challenge/Europa I/rooms.py", line 148, in

myRoom = Room(101, 'myRoom', 'Basic Room', 5, '', myObjects, myExits)

File "/Users/Jasper/Development/Programming/MyProjects/Game Making Challenge/Europa I/rooms.py", line 29, in __init__

if type(self.code) != type(str()):

TypeError: 'str' object is not callable

——谢谢你的帮助,但是:

这适用于isinstance(item,list)还是isinstance(item,tuple)?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值