Skip to content

Commit 085d893

Browse files
committed
note on late binding closures
1 parent 05c17f2 commit 085d893

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

docs/user_guide/geojson/geojson.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,23 @@ colormap.add_to(m)
237237
m
238238
```
239239

240+
>
241+
> **Caveat**
242+
>
243+
> When using `style_function` in a loop you may encounter Python's 'Late Binding Closure' gotcha!
244+
> See https://docs.python-guide.org/writing/gotchas/#late-binding-closures for more info.
245+
> There are a few ways around it from using a GeoPandas object instead,
246+
> to "hacking" your `style_function` to force early closure, like:
247+
> ```python
248+
> for geom, my_style in zip(geoms, my_styles):
249+
> style = my_style
250+
> style_function = lambda x, style=style: style
251+
> folium.GeoJson(
252+
> data=geom,
253+
> style_function=style_function,
254+
> ).add_to(m)
255+
> ```
256+
240257
### Highlight function
241258
242259
The `GeoJson` class provides a `highlight_function` argument, which works similarly

0 commit comments

Comments
 (0)