python - What's the use of a circular reference? -
in python can append list , accept assignment.
>>> l = [0,1] >>> l.append(l) >>> l [0, 1, [...]] >>> l[-1] [0, 1, [...]] my question why?
python allows rather throwing error, because there's potential use or because wasn't seen necessary explicitly forbid behaviour?
is because there's potential use or because wasn't seen necessary explicitly forbid behaviour?
both. lists store references, , there no reason prevent them storing otherwise-valid references.
as potential uses, consider generic top-down-shooter type video game:
- a
levelcontains reference eachenemy, can draw , update them each frame. - an
enemycontains referencelevel, can (for example) query distanceplayeror spawnbulletinlevel.
Comments
Post a Comment