内部阶级从另一张纸条上召唤

2024-04-27 05:24:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发2.7.3python。我用Layer类创建了一个脚本,其中有Sublayer类。我修改了后者的__str__方法,但它不接受修改。你知道吗

代码:

#-*- coding: utf-8 -*-

import numpy as np

class Layer:

     class Sublayer:
        def __init__(self, mask, surface, height):
            self.mask    = mask
            self.surface = surface
            self.height  = height

        def __str__(self):
            print '\n    ---- Couche ---- \n    ----------------\n'

            cc = "  Surface de la couche : "
            cc += str(self.surface)      + "\n  Hauteur : "
            cc += str(self.height) + '\n'
            return cc   

        def __getitem__(self):
            return (self.mask, self.surface, self.height)

然后我创建了另一个脚本,tests脚本:

import numpy as np
from ClassLayer import Layer

l5 = np.array([[0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,150,0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0],
       [0,0,0,0,0,0,  0,0,0,0]])

h5 = np.max([np.max(l5[i]) for i in range(len(l5))])
if h5 != 1:
    s5 = sum(sum(l5))/h5

che5 = Layer.Sublayer(l5,s5,h5)
print che5

因此,在执行打印时,它不会考虑我在Sublayer__str__中添加的新修改。你知道吗

如果可能的话,我如何编写这段代码,使两个脚本保持分离?你知道吗


Tags: 代码importself脚本layerdefnpmask