Access the attributes of inherited PyTorch class in C++ frontend


I am trying to customize the tensor in PyTorch (inherited from torch.Tensor class) referring to this discussion.

For some reasons, I wish to access the attributes from C++ frontend, for instance, MyObject.extra_data in the above reference. One thing that is interesting is that the customized tensor can be loaded as an argument of C++ frontend with torch::Tensor. e.g.,

torch::Tensor load_custom_tensor(torch::Tensor my_obj){
    float* my_obj_ptr = my_obj.data_ptr<float>();
    my_obj_ptr[0] += 1;
    return my_obj;

In the above code, my_obj_ptr points the tensor data, and surely the tensor operation works. When the function is bound with python, the return type in the python code is not torch.Tensor, but MyObject, which means that the customized class, MyObject, is well recognized in C++ frontend. But, I cannot find the way to access python attributes in the customized class as members in C++ frontend object.

How could I access these attributes defined in the python code? If impossible, should I override torch::Tensor class in C++ frontend?