Built-in non-data version of property?

python

Solution

To expand on my comment, why not simply something like this:

>>> class Books():
...     def __init__(self):
...         self.__dict__['referTable'] = 1
...     @property
...     def referTable(self):
...         try:
...             return self.__dict__['referTable']
...         except KeyError:
...             return 2
... 
>>> a = Books()
>>> a.referTable
1
>>> del a.__dict__['referTable']
>>> a.referTable
2

Now, I'd like to note that I don't think this is good design, and you'd be much better off using a private variable rather than accessing `__dict__` directly. E.g:

class Books():
    def __init__(self):
        self._referTable = 1

    @property
    def referTable(self):
        return self._referTable if self._referTable else 2

In short, the answer is no, there is no alternative to `property()` that works in the way you want in the Python standard library.

Problem

``` class Books(): def __init__(self): self.__dict__['referTable'] = 1 @property def referTable(self): return 2 book = Books() print(book.referTable) print(book.__dict__['referTable']) ``` Running: ``` vic@ubuntu:~/Desktop$ python3 test.py 2 1 ``` `Books.referTable` being a data descriptor is not shadowed by `book.__dict__['referTable']`: The `property()` function is implemented as a data descriptor. Accordingly, instances cannot override the behavior of a property. To shadow it, instead of `property` built-in descriptor i must use my own descriptor. Is there a built in descriptor like `property` but which is non-data?

Original source