1

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx for the answer. Do you know what the 'cls' actually does and what it is for?
@Miau It represents the current class, which is PointCloud in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.