I am writing a Python wrapper class for a C# API accessed through Pythonnet. As I want to extend the API with my own methods I decided to wrap it using the composition approach outlined here:
The C# API makes heavy use of properties which I want to mimic in my Python code. The following minimal example shows my current approach for the example of the C# Surface class with the two properties width and height:
class MySurface:
def __init__(api_surface):
self.api_surface = api_surface
@property
def width(self):
return self.api_surface.width
@width.setter
def width(self, value):
self.api_surface.width = value
@property
def height(self):
return self.api_surface.height
@height.setter
def height(self, value):
self.api_surface.height = value
In total, I have to deal with about 50 properties. For several groups of properties I want to add my own error checks, type conversions, etc. What I am looking for is a Pythonic way of defining the properties, e.g. through a factory or using descriptors. Thanks for your help!
Edit: I want to be able to use tab completion within a python shell, i.e. surface. {hit tab} should propose surface.width and surface.height. This does not seem to be possible with the getattr approach outlined by Greg.