I've been doing a lot of work in C# recently (my primary language is Python) and I love that I can do something likepublic string MyProperty{ get; set; }Going back to Python I miss that sort of functionality.  I don't like that I would have to do class MyClass:    @property    def foo(self):        return self._foo    @foo.setter    def foo(self, value):        self._foo = valuea = MyClass()a.foo = 7print(a.foo)I thought that this was something I could do pretty easily with a descriptor, like so.class MyClass:    foo = AutoProperty()a = MyClass()a.foo = 7print(a.foo)Unfortunately, I realized while implementing this that I was violating my favorite design principle - KISS.  After all, what I'd been thinking about were just instance variables.  class MyClass(): passa = MyClass()a.foo = 7print(a.foo)The reason we don't always use properties by default in Python is that, usually, getting and setting instance variables isn't nearly as involved as languages like Java make it - mostly because the concept of private, protected, and public fields don't really exist in Python, except by (weak) conventions and language features, and because changing from a field to a property isn't a breaking change in Python.If we later decide that we do need more complex validation while setting or calculations while getting, then we can easily do that by implementing properties and not affecting the interface of our class.I'm still wondering though - is there any sort of way this technique, or a modified version of this technique, could be used to provide a useful and Pythonic feature?
Could there be a use case for C# style auto-properties in Python
c#;design patterns;python;properties
In Python, switching between an attribute and a property isn't a breaking change, it doesn't alter the interface at all, so there's not much point to an auto-property system. You can start with a public attribute, then later on switch to a property without needing to alter the code that interfaces with your class at all.This is also why defining get_ and set_ methods in Python is discouraged; the property is a much neater way of protecting access, and means that you don't have to implement those methods from the beginning in case you later need them to do something non-trivial. See e.g. Do you use the get/set pattern (in Python)? on SO.If you did decide to implement an AutoProperty you should add __delete__, too; I use the following as a base for other descriptors:class BaseDescriptor(object):    Descriptor base class, providing basic set, get and del methods.    Arguments:      name (str): The attribute to redirect access to (usually the name        to which the descriptor is assigned with a leading underscore,        indicating private-by-convention).    Example:        >>> class TestClass(object):        ...     foo = BaseDescriptor('_foo')        >>> inst = TestClass()        >>> inst.foo = 'bar'        >>> inst.foo        'bar'        >>> del inst.foo        >>> inst.foo        Traceback (most recent call last):          ...        AttributeError: 'TestClass' object has no attribute '_foo'        def __init__(self, name):        self.name = name    def __get__(self, obj, typ=None):        return getattr(obj, self.name)    def __set__(self, obj, val):        setattr(obj, self.name, val)    def __delete__(self, obj):        delattr(obj, self.name)Updated version for review at  https://codereview.stackexchange.com/q/98892/32391