If you want to continue to use get_Field and set_Field for an object that has switched to using properties (where you simply open or assign Field ), you can use a wrapper object:
class NoPropertyAdaptor(object): def __init__(self, obj): self.obj = obj def __getattr__(self, name): if name.startswith("get_"): return lambda: getattr(self.obj, name[4:]) elif name.startswith("set_"): return lambda value: setattr(self.obj, name[4:], value) else: return getattr(self.obj, name)
This will have problems if you use additional syntax, such as indexing or iterating an object, or if you need to recognize the type of an object using isinstance .
A more complicated solution would be to create a subclass that rewrites the name and forces the object to use it. This is not exactly packaging, as external code will still process the object directly (and therefore magic methods and isinstance ) will work as expected. This approach will work for most objects, but it may fail for types that have fantastic metaclass magic, and for some built-in types:
def no_property_adaptor(obj): class wrapper(obj.__class__): def __getattr__(self, name): if name.startswith("get_"): return lambda: getattr(self, name[4:]) elif name.startswith("set_"): return lambda value: setattr(self, name[4:], value) else: return super(wrapper, self).__getattr__(name) obj.__class__ = wrapper return obj
source share