typeerror: descriptors cannot not be created directly. if this call came
时间: 2023-09-18 16:14:35 浏览: 62
from a __get__ operation, then switching to a __set__ operation will create a new property instance. Can you explain what this error means and how to fix it?
This error typically occurs when trying to create a property descriptor directly, without using the built-in `property()` function. To fix this error, you should use the `property()` function to create the descriptor, which will enable you to get and set the value of the property.
For example, instead of directly creating a property descriptor like this:
```python
class MyClass:
def __init__(self, value):
self._value = value
def get_value(self):
return self._value
def set_value(self, value):
self._value = value
my_property = property(get_value, set_value)
```
You should use the `property()` function like this:
```python
class MyClass:
def __init__(self, value):
self._value = value
def get_value(self):
return self._value
def set_value(self, value):
self._value = value
my_property = property(get_value, set_value)
```
This will created a properly formatted property descriptor that can be used to get and set the value of the property.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)