问题描述
我刚开始使用Python,这是关于类的逻辑和实现的非常普遍的问题.对于这个问题的基本水平,我深表歉意,但希望对其他人也有用.这里有一些上下文可以使它更清楚:
I am at the very beginning with Python, and this is a very general question on the logic and implementation of classes. I apologize for the basic level of the question, but hopefully it will be useful for others too. Here is some context to make it clearer:
-
我想创建一个代表图像的类.该图像包括3个波段(R,G,B,与3个不同的文件相关联)和一些元数据(一个文件,其中包含3个波段的文件的路径以及其他信息,例如摄像机,地理位置等).
I want to create a class representing an image. This image includes 3 bands (R,G,B, associated with 3 different files) and some metadata (one file, which contains the file path of the 3 bands files and other info like camera, geo location, etc.).
就我的问题思考方式而言,Image类应包含Metadata类型的属性和Band类型的三个属性.
For my way of thinking the problem, the class Image should include an attribute of type Metadata and three attributes of type Band.
类元数据应具有读取和返回各种信息的方法
Class Metadata should have methods to read and return various info
"Class Band"应具有用于分析和处理每个栅格波段的方法.但是,这些方法可能需要访问元数据中包含的信息.
Class Band should have methods for analysis and processing of each raster band. However, these methods may need to access information enclosed in Metadata.
这就是我要做的:
class Metadata:
def __init__(self, meta_file_path):
self.Path = meta_file_path
def ReadBandPath(self,band_number):
[...]
def ReadLocation(self):
[...]
def ReadCameraInfo(self):
[...]
def GetSomeOtherInfo(self):
[...]
class Band:
def __init__(self,Metadata, band_number):
self.Meta = Metadata
self.Number = band_number
self.Path = self.Meta.ReadBandPath(self.Number)
def DoSomething(self):
self.Meta.GetSomeOtherInfo()
[...]
class Image:
def __init__(self, meta_file_path)
self.Meta = Metadata(meta_file_path)
self.Band1 = Band(self.Meta, 1)
self.Band2 = Band(self.Meta, 2)
self.Band3 = Band(self.Meta, 3)
def SaveAsPng(dest_file):
[...]
我的问题
我的方式对我来说似乎有点多余,更重要的是,它似乎是静态的".看起来如果我在创建Image.BandN之后更新了Image.Meta中的某些信息,那么Image.BandN.Meta将不会同时更新,对吧?
My problem
My way seems a little redundant to me, and more importantly it seems "static". It looks like if I update some info in Image.Meta after creating Image.BandN, then Image.BandN.Meta won't be simultaneously updated, right?
- 我可以正确设置和实施问题吗?
- 以动态方式将Metadata属性传递给Band对象的最聪明的方法是什么?
推荐答案
不,这不是问题; my_image.Band1.Meta
将是对与 my_image.Meta
相同对象的引用.
No, this isn't a problem; my_image.Band1.Meta
will be a reference to the same object as my_image.Meta
.
仅当您重新分配 my_mage.Meta
来命名不同对象(而不是对其命名的对象进行变异)时,您才会遇到问题.
It's only if you reassign my_mage.Meta
to name a different object (as opposed to mutating the object it names) that you'll have a problem.
但是您可以自己打印出 id(my_image.Meta)
和 id(my_image.Band1.Meta)
或检查 my_image,以自己进行测试.元是my_image.Band1.Meta
.
But you can test this yourself, by printing out id(my_image.Meta)
and id(my_image.Band1.Meta)
, or checking my_image.Meta is my_image.Band1.Meta
.
嗯,这有点多余和静态,因为它只处理三个频段,如果您想对CMYK使用相同的代码,则需要在整个地方进行更改.如果那是您可能想做的事情,则可能需要考虑:
Well, it is a bit redundant and static in that it handles exactly three bands, and would need changes all over the place if you wanted to use the same code for, say, CMYK. If that's something you might ever want to do, you might want to consider:
self.Bands = []
self.Bands.append(Band(self.Meta, 1))
self.Bands.append(Band(self.Meta, 2))
self.Bands.append(Band(self.Meta, 3))
或者:
self.Bands = [Band(self.Meta, i) for i in range(3)]
或者,如果RGB是固有的且不可更改的部分,则可能要使用名称而不是数字(仅'R'
','G'
,"B"
).然后,您可能仍然想要将它们放入集合中,而不是将其放入单独的变量中:
Alternatively, if RGB is an inherent and unchangeable part, you may want to use names instead of numbers (just 'R'
', 'G'
, 'B'
). And then, you might still want to put them into a collection instead of separate variables:
self.Bands = {name: Band(self.Meta, name) for name in ('R', 'G', 'B')}
这篇关于一个属性可以访问另一个属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!