【问题标题】:How to return something from a method to a class in Python如何在 Python 中将方法中的内容返回到类中
【发布时间】:2014-08-30 10:00:31
【问题描述】:

我还是 python 新手,我正在尝试编写我的第一个类,它最终将包含一堆图像处理方法。

我的问题是,即使我的方法计算了一些东西,我发现很难将这个结果“返回”给课堂。因此,取决于我放置“return”命令的位置 - 要么我得到一个错误,即方法未使用要么它没有获得对返回变量的引用(未解析的引用)。

代码看起来像这样(未解决参考的版本):

from sklearn.feature_extraction import image
import numpy as np


class ImagePreprocessing():


    def __init__(self, X=None, Y=None,
                 tile_size=None, label_computation_method = "majority"):
        self.X = X
        self.Y = Y
        self.tile_size = tile_size
        self.label_computation_method = label_computation_method


        def image_tilling_odd(X,Y,tile_size,label_computation_method):

          """
            This method takes a set of image data and their respectice tiles and constructs a
            tilebased vector image - with each tile per row - so the overall size is
                (size[0] x size[1] x number of bands)
          """

          # asser if tile dimension is the same is all directions
          assert tile_size[0] == tile_size[1]

          # test if tiles are even or odd
          assert tile_size[0] % 2 != 0
          assert tile_size[1] % 2 != 0


          # tile images and labels
          image_tiles=image.extract_patches_2d(X,tile_size)
          label_tiles=image.extract_patches_2d(Y,tile_size)

          # construct column vector of zeros to store label
          Y=np.zeros(label_tiles.size[0])

          if label_computation_method == "central_value":

              # index of middle element
              idx=tile_size[0]/2 +1

              # detect middle element and store labels --- central element labeling
              for i,rr in enumerate(label_tiles):

                 # construct vector with middle labels
                 Y[i]= rr[idx]

                 COM=np.append(image_tiles,Y,1)

        return COM

【问题讨论】:

  • 请修正您的意图
  • PEP-8 人! :P 对我们来说更容易 C&P ;-)
  • 你到底是什么意思?我不确定我是否得到你!
  • 也附上错误信息。
  • @user1396713:您的image_tilling_odd 缩进就像它是__init__ 方法的一部分一样。正确的缩进至关重要。

标签: python class methods return


【解决方案1】:

我的猜测是你的缩进是错误的,或者你不需要返回任何东西,只需设置一个类属性值。

class MyClass(object):
    def __init__(self, x=0, y=0):
        super().__init__()

        self.x = x
        self.y = y
        self.result = None
    # end Constructor

    def method_return(self):
        return self.x + self.y
    # end method_return

    def method_for_attribute(self):
        self.result = self.x + self.y
    # end method_for_attribute
# end class MyClass

if __name__ == "__main__":
    my_instance = MyClass(1, 1)

    result = my_instance.method_return() # Returns a value to result
    print(result)

    print(my_instance.result)
    my_instance.method_for_attribute() # Sets the value for my_instance.result
    print(my_instance.result)

【讨论】:

    【解决方案2】:

    好的,我想我找到了 - 下面的解决方案!

    from sklearn.feature_extraction import image
    import numpy as np
    
    
    class ImagePreprocessing():
    
    
            def __init__(self, X=None, Y=None,
                     tile_size=None, label_computation_method = "majority"):
               self.X = X
               self.Y = Y
               self.tile_size = tile_size
               self.label_computation_method = label_computation_method
    
    
            def image_tilling_odd(X,Y,tile_size,label_computation_method):
    
              """
                This method takes a set of image data and their respectice tiles and constructs a
                tilebased vector image - with each tile per row - so the overall size is
                    (size[0] x size[1] x number of bands)
              """
    
              # asser if tile dimension is the same is all directions
              assert tile_size[0] == tile_size[1]
    
              # test if tiles are even or odd
              assert tile_size[0] % 2 != 0
              assert tile_size[1] % 2 != 0
    
    
              # tile images and labels
              image_tiles=image.extract_patches_2d(X,tile_size)
              label_tiles=image.extract_patches_2d(Y,tile_size)
    
              # construct column vector of zeros to store label
              Y=np.zeros(label_tiles.size[0])
    
              if label_computation_method == "central_value":
    
                  # index of middle element
                  idx=tile_size[0]/2 +1
    
                  # detect middle element and store labels --- central element labeling
                  for i,rr in enumerate(label_tiles):
    
                     # construct vector with middle labels
                     Y[i]= rr[idx]
    
                     COM=np.append(image_tiles,Y,1)
    
              return COM
    

    【讨论】:

    • 问题是返回函数的位置(对齐)错误!
    猜你喜欢
    • 2022-01-18
    • 2013-05-04
    • 2015-05-20
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多