pycharm运行时报TypeError: Descriptors cannot not be created directly.
时间: 2023-09-19 08:06:21 浏览: 52
这个错误通常是由于在类中直接创建描述符导致的。描述符必须作为类属性来创建,不能在类的方法中动态创建。
以下是一个错误的示例:
```python
class MyDescriptor:
def __get__(self, instance, owner):
pass
class MyClass:
def __init__(self):
self.my_descriptor = MyDescriptor() # 错误:描述符不能直接创建
def my_method(self):
my_descriptor = MyDescriptor() # 错误:描述符不能直接创建
pass
```
正确的做法是将描述符作为类属性创建:
```python
class MyDescriptor:
def __get__(self, instance, owner):
pass
class MyClass:
my_descriptor = MyDescriptor() # 正确:描述符作为类属性创建
def my_method(self):
pass
```
这样就可以避免这个错误了。
相关问题
在pycharm报出这个错误TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other poss
ible causes of this error include:
1. Trying to create a descriptor object directly, instead of using a factory function or constructor.
2. Passing incorrect arguments to the factory function or constructor.
3. Using an outdated version of the protobuf library.
To resolve this issue, you can try the following steps:
1. Make sure that you are using the latest version of the protobuf library. You can upgrade to the latest version using pip:
```
pip install --upgrade protobuf
```
2. If you are still encountering the same error, try regenerating your protobuf code using a newer version of protoc:
```
protoc --version
```
If your version of protoc is older than 3.19.0, you will need to update it. You can download the latest version from the official protobuf website: https://developers.google.com/protocol-buffers
3. If neither of the above steps work, check your code for any instances where you are directly creating descriptor objects. Instead, use the appropriate factory function or constructor to create descriptor objects.
pycharm报错TypeError: must be real number, not str
这个错误通常是因为你在代码中使用了字符串(str)而应该使用实数(real number)。你可以在代码中查找到使用字符串的地方并将其替换为实数。
比如,如果你的代码中有这样一句话:
```
x = '3.14'
y = x + 2
```
那么你需要将 x 的值修改为实数类型的值:
```
x = 3.14
y = x + 2
```
如果你不确定哪里出了问题,可以在错误提示中找到报错的行数,从那里开始检查代码。