please define the function outside of the forloop, eg:

```python
f = lambda x: x[metadata_category][:level]
for bin, table in biom_table_norm.binObservationsByMetadata(f):
    # do stuff
```