简体   繁体   English

为什么在循环时出现TypeError?

[英]Why am I getting a TypeError when looping?

I'm working on a Python extension module, and one of my little test scripts is doing something strange, viz.: 我正在使用Python扩展模块,而我的一些小测试脚本之一正在做一些奇怪的事情,即:

x_max, y_max, z_max = m.size

for x in xrange(x_max):
    for y in xrange(y_max):
        for z in xrange(z_max):
            #do my stuff

What makes no sense is that the loop gets to the end of the first 'z' iteration, then throws a TypeError, stating that "an integer is required". 没有意义的是,循环到达第一个“ z”迭代的结尾,然后引发TypeError,指出“需要整数”。 If I put a try...except TypeError around it and check the types of x, y, and z, they all come back as < type 'int' >. 如果我尝试一下...除TypeError之外,然后检查x,y和z的类型,它们都将以<type'int'>的形式返回。

Am I missing something here? 我在这里想念什么吗?

EDIT: It appears I've got a problem somewhere in my extension code. 编辑:看来我的扩展代码中某处有问题。 Pulling out those lines one by one revealed the culprit. 一条一条地拉出那些线,是罪魁祸首。 I suspect a reference counting error. 我怀疑参考计数有误。 Thanks for the replies. 感谢您的答复。

The problem is here: x_max, y_max, z_max = m.size 问题在这里: x_max, y_max, z_max = m.size

This syntax x_max, y_max, z_max expects a tuple/list on the other end of the equality sign so unless m.size is one -- and I take it it's an integer -- you need the following: 这种语法x_max, y_max, z_max期望在等号的另一端有一个元组/列表,因此,除非m.size为一(我认为它是整数),否则您需要:

x_max = y_max = z_max = m.size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM