2021年1月28日星期四

Changing an object's attribute in a list during iteration with Python

In Python, I'm trying to use nested for loops in my script to iterate over a list of object attributes. The iterated copy of the attribute resets during nested iteration. How can I modify the referenced object's attribute so that it doesn't reset?

class Player:      def __init__(self, index, name, sex, bench)        player1 = Player(1, "john", "male", True)        player2 = Player(2, "bill" "male", True)        roster = [player1, player2, player3]        for x in roster            x.bench = False  

How can I modify the object attribute (player1.bench) to match the iteration value (x.bench)?

https://stackoverflow.com/questions/65947397/changing-an-objects-attribute-in-a-list-during-iteration-with-python January 29, 2021 at 09:01AM

没有评论:

发表评论