简体   繁体   English

为什么会出现此TypeError?

[英]Why am I getting this TypeError?

RESOLVED : Okay, you guys probably won't believe this. 解决 :好的,你们可能不会相信这一点。 I did a lot of digging and it turns out that all the files we are loading and using were created incorrectly. 我做了很多挖掘工作,结果发现我们正在加载和使用的所有文件都是错误创建的。 The files fail to conform with the code we are writing — the things we want to do in our program are simply not possible based on the current state of the files we load. 这些文件不符合我们正在编写的代码-根据加载的文件的当前状态,根本无法在程序中执行我们想做的事情。 I am currently working on fixing this. 我目前正在解决此问题。 Sorry about the non-question, guys! 抱歉,没问题,伙计们!


In Python I have code that essentially reads as follows: 在Python中,我的代码基本上如下所示:

partsList = getPartsList() # this function returns a list
for part in partsList:
    ...
bar(partsList)

def bar(partsList):
    for part in partsList:
        ...

But when I run the code I get the following TypeError: 但是,当我运行代码时,出现以下TypeError:

TypeError: iteration over non-sequence

This TypeError is in reference to the noted line: TypeError参考所提到的行:

def bar(partsList):
    for part in partsList: # this is the line with the TypeError
        ...

How can this be? 怎么会这样? I know that partsList is not a non-sequence because just before my program calls bar(partsList) , I explicitly iterate over partsList . 我知道partsList 不是非序列的,因为在我的程序调用bar(partsList) ,我明确地遍历了partsList

My function does not modify partsList before interacting with it, and I do not modify partsList when iterating through it prior to calling the function, yet somehow it changes from a list to a non-sequence when the function is called. 我的函数在与之交互之前不会修改partsList ,并且在调用函数之前partsList进行迭代时也不会修改partsList ,但是在调用该函数时,它以某种方式从列表变为非序列。

I am working entirely within a class so these are all methods actually; 我完全在一个类中工作,所以实际上这些都是方法。 I just thought it would be easier to read if I present the code this way. 我只是认为,如果以这种方式显示代码,将更容易阅读。


The following is in response to the comments: 以下是对评论的回应:

I wish I could provide you all with the full code, but at the moment the program requires exactly 275 files to run and has 20+ .py files. 我希望可以为大家提供完整的代码,但是目前该程序需要运行275个文件,并且有20多个.py文件。 I will mention that the method in question does employ recursion after iteration through its given list. 我将提到,所讨论的方法在遍历给定列表确实采用了递归。 I thought this may be linked to the error, but when when attempting to print the list itself and its contents, the program gave the same TypeError before making it through the method even once, so I know that this is not due to the recursion; 我以为这可能与错误有关,但是当尝试打印列表本身及其内容时,程序甚至通过该方法一次之前就给出了相同的TypeError ,因此我知道这不是由于递归引起的。 it never even recursed. 它甚至从未复发。


Ok I have inserted print statements as follows (keep in mind these are within methods in a class): 好的,我已经按如下方式插入了打印语句(请记住,这些语句在类的方法中):

def someMethod(self):
    ...
    partsList = self.getPartsList() # this function returns a list
    for part in partsList:
        ...
    print partsList                      # prints [object(1), object(2)]
    self.bar(partsList)

def bar(self, partsList):
    print partsList        # prints <filename.class instance at 0x04886148>
    for part in partsList: # still gives me the TypeError
        ...

When I say filename.class I don't literally mean filename and class . 当我说filename.class我的意思不是filenameclass You guys know what I mean. 你们知道我的意思。

Is the second print statement printing <filename.class instance at 0x04886148> because it is pointing to the actual partsList ? 第二个打印语句是否正在打印<filename.class instance at 0x04886148>因为它指向实际的partsList I'm not entirely sure how pointers and references work in Python. 我不完全确定指针和引用在Python中如何工作。

You don't define bar correctly; 您没有正确定义bar its first argument is a reference to the object that calls it, and the second argument is the list you pass as the explicit argument. 它的第一个参数是对调用它的对象的引用, 第二个参数是您作为显式参数传递的列表。

def bar(self, partsList):
    for part in partsList:
        ...

Your answer is there in the print lines. 您的答案在打印行中。

def bar(self, partsList):
    print partsList        # prints <filename.class instance at 0x04886148>
    for part in partsList: # still gives me the TypeError
        ...

partsList isn't a list going into this method. partsList不是该方法的列表。 Here is some tweaked, functioning, example code from your code: 这是您代码中经过调整的可正常运行的示例代码:

class myClass():
    def someMethod(self):
        partsList=self.getPartsList()
        for part in partsList:
            print part
        self.bar(partsList)

    def bar(self, pList):
        print pList
        for part in pList:
            print part

    def getPartsList(self):
        return ['a', 'b', 'c']

Running this interactively gets me this: 交互式运行此命令可获得以下信息:

from fake_try import myClass x = myClass() x.someMethod() abc ['a', 'b', 'c'] abc 从fake_try导入myClass x = myClass()x.someMethod()abc ['a','b','c'] abc

You'll notice that when I called "print pList" I received a pretty print output of the list. 您会注意到,当我调用“ print pList”时,我收到了列表的漂亮打印输出。 You are receiving an object of your class type. 您正在接收您的类类型的对象。

I understand and empathize with your situation. 我了解并理解您的情况。 Having a large, complex program throwing errors can be quite painful to debug. 拥有大型复杂程序引发的错误可能很难调试。 Unfortunately without seeing your entire code I don't think anyone here will be able to debug your issue because my guess is that you are calling 'someMethod' in a way that is unexpected in the actual code(or in an unexpected place) which is causing you to have issues. 不幸的是,没有看到您的完整代码,我认为这里的任何人都无法调试您的问题,因为我猜您正在以某种在实际代码(或意外位置)中意外的方式调用“ someMethod”导致您遇到问题。

There are a couple of ways you can debug this. 您可以通过两种方法来调试它。

  1. I am assuming that everything ran UNTIL you added the someMethod functionality? 我假设一切都运行了,直到您添加了someMethod功能? Revert your code to a state prior to the error and add lines on at a time(with dummy functions if neccesary) to find exactly where the unexpected value is coming from. 将您的代码恢复为错误之前的状态,并一次添加行(如果需要,可以使用伪函数)以查找确切值出自何处。 If you cannot revert my first step would be to simplify all logic surrounding this issue. 如果您无法还原,我的第一步将是简化围绕此问题的所有逻辑。 You have a function 'getPartsList()' that's supposed to return a list. 您有一个函数“ getPartsList()”,该函数应该返回一个列表。 It looks like it is here, but make it even easier to check. 看起来好像在这里,但是使检查变得更加容易。 Make a dummy function that simply returns a fake list and see what the behavior is. 创建一个仅返回假列表并查看其行为的虚拟函数。 Change things one step at a time until you iron out where the issue is. 一次更改一个步骤,直到解决问题所在。

  2. You may not be familiar with the inspect module. 您可能不熟悉检查模块。 Try importing inspect in your module and using inspect.getmember(x) with x being the object you want more information about. 尝试将检查导入到模块中,并使用inspect.getmember(x),其中x是您想要了解更多信息的对象。 I would probably use this in place of you print partsList in the bar method( something like inspect.getmember(partsList) ) I would guess that you're somehow passing a class there instead of the list, this should tell you what that class has been instantiated as. 我可能会用它代替您在bar方法中打印partsList(类似inspect.getmember(partsList)),我想您是在以某种方式传递了一个类而不是列表,这应该告诉您该类有什么被实例化为。

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

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