#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    Object is Iterable, Then Not Iterable


    Clearly I'm confused as my code appears to iterate properly, then I receive the error:

    "TypeError: 'NoneType' object is not iterable"

    This is essentially what I have:

    Code:
    def findOriginalDBMatch(newPartDef, checkIndex, dbPartDef):
    
        for j in range(len(dbPartDef)):
            if dbPartDef[j].isGeometry():
                print(dbPartDef[j].getPartName())
                if newPartDef[checkIndex].getClosestMatch() == dbPartDef[j].getNameSweep1():
                    return j, dbPartDef[j].getPartName()
    
    for i in range(len(newPartDefinitions)):
        print(len(dbFullPartDefinitions))
        if newPartDefinitions[i].isGeometry():
            #print(newPartDefinitions[i].getPartName())
            matchIndex, matchName = findOriginalDBMatch(newPartDefinitions, i, dbFullPartDefinitions)
    The last line with 'matchIndex, matchName' is causing the error. I am confused because the print command in the 'findOriginalDBMatch' function prints many times before the program crashes. In the function call, 'newPartDefinitions' and 'dbFullPartDefinitions' are lists of objects.

    I'm at a loss right now, so please let me know what other info is helpful.

    Thanks.

    -John
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    I think I have figured it out - in certain cases the function did not have inputs that met criteria for it to return something. The error message seems a little confusing/misleading to me.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Python functions always return something.
    If you don't specify the "something" the return value is None .
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    Thanks for the response. So the "'NoneType' object" that the error message refers to actually is what is returned by the function? Is it saying it should be iterable because I am expecting the function to return multiple values?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    >>> (a,b,) = None
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'NoneType' object is not iterable

    Originally Posted by John
    Is it saying it should be iterable because I am expecting the function to return multiple values?
    Likely!
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    9
    Rep Power
    0

    typo


    if you want a function to return multiple values, be itterable.
    you have to use yield instead of return.

    Code:
    def testItter():
      for i in range(1,10):
        yield i
    
    for i in testItter():
      print i

IMN logo majestic logo threadwatch logo seochat tools logo