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

What would be the best practice to create a new object, which uses the attributes of an existing object of another class type in Python?

Let’s say I have an object MvsObject of the class MvsClass and I want to create a new object of a different class that uses the attributes densePointClouds and sparsePointClouds and processes them with the methods of the class PointCloud.

Would the following approach be a “good practice” in Python?

class PointCloud:    

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

Answer

Your solution is good. You could also use @classmethod decorator in order to define two ways to build your class (in a “classical” way, or using another instance).

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)

You would instantiate it like this:

point = PointCloud.from_mvs_object(mvs_object)

Note also I renamed the attributes because using Python, it’s preferred to use snake case to name your variables.