{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreifoylbmy4qhbd7hogwm6di27wgkqhibgomvghevfhdd2gxkbfzqxi",
    "uri": "at://did:plc:6dmfe46c76jjenq3kaxc5eds/app.bsky.feed.post/3meulcpcilqw2"
  },
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreiceca7bjbqzy6m65wmg3t3d777brvlputl52lcylvk4dt6sqgtm4e"
    },
    "mimeType": "image/png",
    "size": 1114223
  },
  "path": "/website/blog/python-mutable-reference-caching/",
  "publishedAt": "2026-02-14T18:08:23.000Z",
  "site": "https://soumyadghosh.github.io",
  "tags": [
    "@cached"
  ],
  "textContent": "So, while working with caching and scrapping, I understood the difference between immutable and mutable objects/datatypes very clearly. I had a scenario, where I am webscraping an API, the code looks like this.\n\n\n    from aiocache import cached\n\n    @cached(ttl=7200)\n    async def get_forecast(station_id: str) -> list[dict]:\n     data: dict = await scrape_weather(station_id)\n     # doing some operation\n     return forecasts\n\n\nand then using this utility tool in the endpoint.\n\n\n    async def get_forecast_by_city(\n     param: Annotated[StationIDQuery, Query()],\n    ) -> list[UpcomingForecast]:\n     forecasts_dict: list[dict] = await get_forecast(param.station_id)\n     forecasts_dict.reversed()\n\n     forecasts: deque[UpcomingForecast] = deque([])\n     for forecast in forecasts_dict:\n     date_delta: int = (\n     date.fromisoformat(forecast[\"forecast_date\"]) - date.today()\n     ).days\n     if date_delta <= 0:\n     break\n     forecasts.appendleft(UpcomingForecast.model_validate(forecast))\n\n     return list(forecasts)\n\n\nBut, here is the gotcha, something I was doing inherently wrong. Lists in python are mutable objects. So, reversing the list modifies the list in place, without creating a new reference of the list. My initial approach was to do this",
  "title": "Python Mutable References with Caching"
}