6

I have a class named Factor in the module Factor.py (https://github.com/pgmpy/pgmpy/blob/dev/pgmpy/factors/Factor.py) and also have function named factor_product in Factor.py as:

def factor_product(*args):
    if not all(isinstance(phi, Factor) for phi in args):
            raise TypeError("Input parameters must be factors")
    return functools.reduce(lambda phi1, phi2: _bivar_factor_operation(phi1, phi2,     
                                                            operation='M'), args)

Now if I even pass instances of Factor to the function, it still throws TypeError. A few lines from the debugger with breakpoint set just above the if statement:

(Pdb) args
args = (<pgmpy.factors.Factor.Factor object at 0x7fed0faf76a0>, <pgmpy.factors.Factor.Factor object at 0x7fed0faf7da0>)

(Pdb) isinstance(args[0], Factor)
False

(Pdb) type(args[0])
<class 'pgmpy.factors.Factor.Factor'>

(Pdb) Factor
<class 'pgmpy.factors.Factor.Factor'>

Any idea why this is happening ?

4

2 回答 2

6

reload是从同一个模块中获得同一个类的两个副本的好方法:一个来自重新加载之前(如果该类的任何实例仍然潜伏),另一个来自之后。

很可能您有新类型的对象,但Factor引用了旧类型,因为它是前一段时间导入的。因此,无论如何,您的对象不是Factor... 不是that的实例,这是完全正确的。 Factor

永远不要相信reload。:)

于 2014-12-14T07:54:57.437 回答
0

As isinstance如果 object 参数是 classinfo 参数或其(直接、间接或虚拟)子类的实例,则返回 true,如果您将类的实例而不是类本身传递给它,它只会返回 true ,请参阅以下示例:

>>> class A :
...  pass
... 
>>> isinstance(A,A)
False
>>> isinstance(A(),A)
True
>>> z=A()
>>> isinstance(z,A)
True 
于 2014-12-14T07:03:16.543 回答