简体   繁体   English

isinstance()因未知原因返回False

[英]isinstance() return False for unknown reason

So, here is the situation: 那么,情况如下:
I'm using GAE Pipeline lib for the flow, my class FanoutPipeline is derived from Pipeline class from the library: 我正在使用GAE Pipeline lib进行流程,我的类FanoutPipeline来自库中的Pipeline类:

from pipeline import pipeline
class FanoutPipeline(pipeline.Pipeline):

When the pipeline process is run, it does a check to verify that the object is actually an instance of a class derived from Pipeline, but it returns False: 运行管道进程时,它会检查以验证该对象实际上是从Pipeline派生的类的实例,但它返回False:

  yielded = pipeline_iter.send(next_value)
  # here I check that yielded is an object of `FanoutPipeline`
  # class, plz see logs output below
  if isinstance(yielded, Pipeline):  # returns false here

I added a ton of logs to get class tree and their modules: 我添加了大量的日志来获取类树及其模块:

  import inspect
  logging.debug('Yielded: {}'.format(yielded))
  for cls in inspect.getmro(yielded.__class__):
      logging.debug('Yielded base: {}'.format(inspect.getmodule(cls)))
      logging.debug('Yielded base: {}'.format(cls))
  logging.debug('PipelineMeta classes: {}'.format(_PipelineMeta._all_classes))
  logging.debug('Pipeline: {}'.format(Pipeline))
  logging.debug('Pipeline module: {}'.format(inspect.getmodule(Pipeline)))
  for cls in inspect.getmro(Pipeline):
    logging.debug(inspect.getmodule(cls))
  if isinstance(yielded, Pipeline):

And got the output: 得到了输出:

 D 21:55:48.079 Yielded: project.handlers.pipeline_gcm.FanoutPipeline(*(None, {u'campaign_id': u'xxx', u'campaign_name': u'xmas notification', u'execution': {u'action': u'market', u'conditions': {u'delayBetweenNotificationsHours': 0, u'in... (515 bytes), **{}) D 21:55:48.079 Yielded base: <module 'project.handlers.pipeline_gcm' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/projects/handlers/pipeline_gcm.pyc'> D 21:55:48.079 Yielded base: <class 'project.handlers.pipeline_gcm.FanoutPipeline'> D 21:55:48.080 Yielded base: <module 'pipeline.pipeline' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/libraries/pipeline/pipeline.pyc'> D 21:55:48.080 Yielded base: <class 'pipeline.pipeline.Pipeline'> D 21:55:48.080 Yielded base: <module '__builtin__' (built-in)> D 21:55:48.080 Yielded base: <type 'object'> D 21:55:48.081 PipelineMeta classes: [<class 'libraries.pipeline.pipeline.Pipeline'>] D 21:55:48.081 Pipeline: <class 'libraries.pipeline.pipeline.Pipeline'> D 21:55:48.081 Pipeline module: <module 'libraries.pipeline.pipeline' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/libraries/pipeline/pipeline.pyc'> D 21:55:48.082 <module 'libraries.pipeline.pipeline' from '/base/data/home/apps/s~project-dev3/9.389913797024223872/libraries/pipeline/pipeline.pyc'> D 21:55:48.082 <module '__builtin__' (built-in)> 

As you see, yielded has clearly pipeline.pipeline.Pipeline in base classes, the module paths match. 如您所见,yielding在基类中显然有pipeline.pipeline.Pipeline ,模块路径匹配。

Here are some ideas why it could happen: 1. The object is passed somehow between processes, and the check fails since one base class is loaded in one process, the other - in the other. 以下是它可能发生的一些想法:1。对象在进程之间以某种方式传递,并且检查失败,因为一个基类在一个进程中加载​​,另一个基类在另一个进程中加载​​。 2. There is a difference of relative paths, ie pipeline.pipeline module in process where object is created 2.相对路径存在差异,即创建对象的进程中的pipeline.pipeline模块
and libraries.pipeline.pipeline in the process where it's used and the isinstance check is being done. 和libraries.pipeline.pipeline在使用它的过程中并且正在进行isinstance检查。

Could you please help me with this issue? 你能帮我解决这个问题吗? What should I try in the first place? 我应该首先尝试什么? What could be the reason to this failure? 这次失败的原因是什么?

You are importing two versions of the class, one as libraries.pipeline.pipeline.Pipeline and another as pipeline.pipeline.Pipeline . 您正在导入该类的两个版本,一个作为libraries.pipeline.pipeline.Pipeline ,另一个作为pipeline.pipeline.Pipeline That would mean you have both /base/data/home/apps/s~project-dev3/9.389913797024223872/ and /base/data/home/apps/s~project-dev3/9.389913797024223872/libraries in sys.path . 这意味着你在sys.path同时拥有/base/data/home/apps/s~project-dev3/9.389913797024223872//base/data/home/apps/s~project-dev3/9.389913797024223872/libraries Although you know they are just two paths to the same module, python doesn't know that and treats them as two different classes. 虽然你知道它们只是通往同一模块的两条路径,但python并不知道它并将它们视为两个不同的类。

You'll need to decide whether you want to base your imports on libraries or the various modules inside libraries and then write your imports consistently. 你需要决定是否要立足于您的进口libraries或内部的各种模块libraries ,然后一直写你的进口。 You should also stop adding the "unchosen" directory to sys.path so that importing the module in the wrong way, fails. 您还应该停止向sys.path添加“unchosen”目录,以便以错误的方式导入模块失败。

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

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