2016-02-05 86 views
2

我为什么下面的代码不起作用困惑:嵌套继承的问题 - 如何初始化父类?

class ComparativeAnnotatorConfiguration(HashableNamespace): 
    """ 
    Takes the initial configuration from the main driver script and builds paths to all files that will be produced 
    by these tasks. 
    """ 
    def __init__(self, args, gene_set, query_genome_files, target_genome_files, annot_files, transmap): 
     self.work_dir = os.path.join(args.workDir, 'comparativeAnnotator', gene_set.sourceGenome, gene_set.geneSet) 
     self.metrics_dir = os.path.join(args.outputDir, 'metrics') 
     self.tx_set_dir = os.path.join(args.outputDir, 'tm_transcript_set') 
     self.reference = self.Reference(args, query_genome_files, annot_files, self.work_dir) 
     self.transmap = self.TransMap(args, query_genome_files, target_genome_files, annot_files, transmap, self.work_dir) 

    class Reference(HashableNamespace): 
     """ 
     The args object that will be passed directly to jobTree 
     """ 
     def __init__(self, args, query_genome_files, annot_files, out_dir): 
      self.__dict__.update(vars(args.jobTreeOptions)) 
      self.outDir = out_dir 
      self.refGenome = query_genome_files.genome 
      self.refFasta = query_genome_files.genome_fasta 
      self.sizes = query_genome_files.chrom_sizes 
      self.annotationGp = annot_files.gp 
      self.gencodeAttributes = annot_files.attributes 
      self.mode = 'reference' 

    class TransMap(Reference): 
     """ 
     The args object that will be passed directly to jobTree 
     """ 
     def __init__(self, args, query_genome_files, target_genome_files, annot_files, transmap, out_dir): 
      super(self.__class__, self).Reference.__init__(self, args, query_genome_files, annot_files, out_dir) 
      self.genome = target_genome_files.genome 
      self.psl = transmap.psl 
      self.refPsl = annot_files.psl 
      self.targetGp = transmap.gp 
      self.fasta = target_genome_files.fasta 
      self.mode = 'transMap' 

试图实例化导致的错误:

AttributeError: 'super' object has no attribute 'Reference' 

我已经尝试了不同的版本,如super(TransMap, self).Reference.__init__Reference.__init__,但所有给不同版本的NameError。如何比这里列出的简单的情况下,这种不同:

Using super() in nested classes

+2

'超(个体经营.__ class__,个体经营)。 __init__ ...'应该足够了。 'super'的结果已经是'Reference' – fjarri

+2

不,不要将'self____ class__'传递给'super()'。如果你将这个类继承下来,它会变得糟糕。 – Kevin

+0

你有没有试过这个超级(TransMap,self).__ init __(#你的arg) – dlmeetei

回答

3

你想这样的:

super(ComparativeAnnotatorConfiguration.TransMap, self).__init__(...) 

这是Python的类作用域规则的结果:类变量不在里面方法的范围。这并不仅仅因为你的“变量”本身就是一个类。就Python而言,这完全一样。

在Python 3,您可以编写简单得多:

super().__init__(...) 

这是又一个理由去升级。

-1
super(self.__class__, self).__init__() 

将调用父类的__init__方法。

+3

**不要那样做**。如果您继承当前课程,它会行为不端。 'super()'的第一个参数应该(几乎)总是一个导入时间常量,而不是通过检查'self'找出的东西。 – Kevin

+0

@Kevin,是的,我明白了,对不起,我不是这个意思,是的,还有其他的缺点,使用它,是直接使用'super().__ init __(...)但仍然感谢让我正确 –

0

你可以使用super(ChildClass, self).__init__()

class BaseClass(object): 
    def __init__(self, *args, **kwargs): 
     pass 

class ChildClass(BaseClass): 
    def __init__(self, *args, **kwargs): 
     super(ChildClass, self).__init__(*args, **kwargs) 

样品继承和初始化父类的构造代码:

class Car(object): 
    condition = "new" 

    def __init__(self, model, color, mpg): 
     self.model = model 
     self.color = color 
     self.mpg = mpg 

class ElectricCar(Car): 
    def __init__(self, battery_type, model, color, mpg): 
     self.battery_type=battery_type 
     super(ElectricCar, self).__init__(model, color, mpg) 

car = ElectricCar('battery', 'ford', 'golden', 10) 
print car.__dict__ 

下面是输出:

{'color': 'golden', 'mpg': 10, 'model': 'ford', 'battery_type': 'battery'}