我想“标记”派生类的属性(它们在其他方面是相同的),以便父类的方法可以使用特定的类。从父类中调用任意的子属性?
在这个例子中,我构建了神经元模型,每个神经元由“区域”组成,而“区域”又由“片段”组成。有一个neuron_region父类。 neuron_region父类有一个“连接”方法,它将一个段连接到另一个段(作为参数传递给另一个神经元)。需要有一种标记派生类中的哪个段需要连接到的方法。什么是一个优雅的方式来做到这一点?
class neuron_region(object):
def connect(external_segment)
#connect segment 1 or segment 2 to external_segment,
#depending on which one is the right attribute
class child1(parent):
#mark segment 1 as the segment to which to connect#
self.seg1='segment 1'
self.seg2='segment 2'
class child2(parent):
self.seg1='segment 1'
#mark segment 2 as the segment to which to connect#
self.seg2='segment 2'
我很难理解你想要做什么! :( – 2011-05-01 20:46:25
@Deniz - 我已经改进了一些代码,使其更加清晰,评论是我想“能够做到的。基本上每个派生类将有许多段,但只有其中一个应该作为其他部分连接到的那个。 – 2011-05-01 21:00:10