Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 内部类\uuuu str\uuuuu从另一个脚本调用_Python_String_Python 2.7_Methods - Fatal编程技术网

Python 内部类\uuuu str\uuuuu从另一个脚本调用

Python 内部类\uuuu str\uuuuu从另一个脚本调用,python,string,python-2.7,methods,Python,String,Python 2.7,Methods,我正在使用2.7.3 python。我用层类创建了一个脚本,其中有子层类。我修改了后者的\uuu-str\uu方法,但它不接受这些修改 守则: #-*- coding: utf-8 -*- import numpy as np class Layer: class Sublayer: def __init__(self, mask, surface, height): self.mask = mask self

我正在使用2.7.3 python。我用
类创建了一个脚本,其中有
子层
类。我修改了后者的
\uuu-str\uu
方法,但它不接受这些修改

守则:

#-*- 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
因此,在执行打印时,它不考虑我在
子层的
中添加的新修改


如果可能的话,如何编写这段代码,将两个脚本分开?

注意,虽然这不是问题的原因,但嵌套类在Python中很少有用;层应该只是一个模块。你说的“注册脚本”是什么意思?你是怎么运作的?可能您的IDE(或任何您的环境)正在缓存
ClassLayer
模块的旧版本。顺便说一句,因为您使用的是Python2,所以应该将类定义为新样式的类,例如
类子层(对象):
。否则,您将得到低级的旧样式类(在Python 3中不再存在)。您是否修复了测试脚本中的import语句,使其使用新名称?。例如,
从类层导入子层
,并使用
che5=子层(l5、s5、h5)
创建实例。如果您仍然有困难,您应该编辑问题以向我们展示您的新代码。顺便说一句,您的
\uuu str\uuu
方法有点奇怪:它应该只返回表示实例的用户友好字符串,它不应该有副作用,比如打印。您的
\uuu getitem\uuuu
甚至更奇怪:比如说,它应该使用
arg。容器类使用此方法实现索引;我还注意到,如果
h5==1,你没有定义
s5
;你需要解决这个问题。此外,您可以更有效地进行这些计算:
h5=np.max(l5)
s5=np.sum(l5)