Closed hagenw closed 3 years ago
Yes, you should never change an object that you iterate over in Python. Copying (e.g. to a list as you do here) is best practise in such cases. So I would not call this a bug, it's how Python is designed.
What it makes it a bit hard to spot here is that we usually use db[...]
notation to access tables. If you rewrite your example to:
db = audformat.testing.create_db()
for table in db.tables:
db.tables[f'{table}.new'] = audformat.Table()
it's more obvious of course.
What it makes it a bit hard to spot here is that we usually use
db[...]
notation to access tables. If you rewrite your example to:db = audformat.testing.create_db() for table in db.tables: db.tables[f'{table}.new'] = audformat.Table()
it's more obvious of course.
That's true. I know there would be a reason to not use short cut notation for tables :)
But otherwise it is very convenient of course.
So, I agree that this is not a bug and will close this.
Try the following:
This results in:
A work around is to first get a list of tables:
This works as expected.
As this is very hard to debug for a user, I think we should try to fix it to also work when directly iterating through
db.tables
.I haven't tested it for other entries, but I guess it might be the same for
db.schemes
etc.