根据 Python 中的另一个对象属性创建 Class 的对象

Create Object of a Class based on another Objects Attributes in Python

创建新对象的最佳做法是什么,该对象使用 Python 中另一种 class 类型的现有对象的属性?

假设我有一个 class MvsClass 的对象 MvsObject,我想创建一个使用属性 class 的不同 class 的新对象15=] 和 sparsePointClouds 并用 class PointCloud.

的方法处理它们

以下方法是否是 Python 中的“良好做法”?

class PointCloud:    

    def __init__(self, MvsObject):
        self.densePointClouds           = MvsObject.densePointClouds
        self.sparsePointClouds          = MvsObject.sparsePointClouds

你的解决方案很好。您还可以使用 @classmethod 装饰器来定义两种构建 class 的方法(以“classical”方式,或使用另一个实例)。

class PointCloud:    

    def __init__(self, dense_points_cloud, sparse_points_cloud):
        self.dense_points_cloud = dense_points_cloud
        self.sparse_points_cloud = sparse_points_cloud

    @classmethod
    def from_mvs_object(cls, mvs_object):
        return cls(mvs_object.dense_points_cloud, mvs_object.sparse_points_cloud)

您可以像这样实例化它:

point = PointCloud.from_mvs_object(mvs_object)

另请注意,我重命名了属性,因为使用 Python,最好使用 snake case 来命名变量。