str object has no attribute setex
时间: 2023-06-21 11:08:45 浏览: 89
Python 从attribute到property详解
The error message "str object has no attribute setex" means that you're trying to call the setex method on a string object, but this method is not defined for strings.
The setex method is a Redis command that sets a value in a Redis database with an expiration time. To use this method in Python, you need to first connect to a Redis server using a Redis client library such as redis-py, and then call the setex method on the Redis client object.
Here's an example of how to use the setex method with redis-py:
```python
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.setex('mykey', 60, 'myvalue')
```
This code connects to a Redis server running on localhost:6379, selects database 0, and then sets the key 'mykey' with the value 'myvalue' and an expiration time of 60 seconds.
Make sure you have installed redis-py library before running this code.
阅读全文