内部类__str__从另一个脚本调用

时间:2017-05-30 13:16:06

标签: python string python-2.7 methods

我正在研究2.7.3 python。我用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)

然后我创建了另一个脚本,即测试脚本:

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

因此,在执行打印时,它不会考虑我在__str__的{​​{1}}中添加的新修改。

如何编写此代码,尽可能将两个脚本分开?

0 个答案:

没有答案