tags:

views:

94

answers:

1

I'm working on a Python extension module, and one of my little test scripts is doing something strange, viz.:

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". 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' >.

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.

A: 

The problem is here: 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

kaloyan
If `m.size` wasn't a sequence type, he would have gotten a `TypeError` before starting the loop.
Seth
Ah, yes. I guess I missed that part.
kaloyan