为什么我的函数在理论上不应该覆盖变量? (Python 3.8)
Why is my function overwriting a variable, when it's theoretically not supposed to? (Python 3.8)
一个函数正在覆盖一个作为参数给它的变量。为什么?
这整个代码应该做什么:
- 创建代表卡片的对象列表(主牌组)
- 创建一个空列表用于存储已经从主牌组抽取的牌的 ID
- 从主牌组中随机选择 5 张牌并将它们添加到列表(玩家的牌组)
- return 变量的临时整数列表(主牌组中卡片的 ID)
将临时列表中的项目添加到主列表
import random
from common import cards
deck = cards.deck() # creates a list of 52 class objects representing cards
def main():
used_cards_ids = [] # this one is being overwritten in the next step, according to the debugger
players_deck, temp_used_cards_ids = generate_players_deck(used_cards_ids) # this is the "next step"
used_cards_ids.extend(temp_used_cards_ids) # adds id of the cards which were drawn from the main deck
print(used_cards_ids) # prints a double list of cards ids (i.e 1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
def generate_players_deck(temp_used_cards_ids):
players_deck = []
counter = 0
while counter < 5: # until 5 cards were drawn
cards_id = random.randint(0, 51) # chooses a random card
if cards_id not in temp_used_cards_ids: # checks if the card with the same id weren't previously drawn
counter += 1
temp_used_cards_ids.append(cards_id) # adds card's id to the list of drawn cards
players_deck.append(deck[cards_id]) # adds card to the player's deck
else:
continue
return players_deck, temp_used_cards_ids # returns player's deck (list of cards objects) and list of ids of the drawn cards
main()
当您将 used_cards_ids
传递给 generate_players_deck()
时,您正在向 used_cards_ids
提供 generate_players_deck()
到 append
数据的主体,您已经别名 到 temp_used_cards_ids
。
然后您从该函数的 return
语句中取回值,您将其存储在变量 temp_used_cards_ids
中,然后 extend
temp_used_cards_ids
,这实际上是 used_cards_ids
,实质上改变了您两次传入的列表。
当您将列表作为函数参数传递时,您传递的不是列表的值,而是传递对原始列表的引用list. 这可能是您感到困惑的地方,或者可能是变量名和函数参数别名之间的切换。
一个函数正在覆盖一个作为参数给它的变量。为什么?
这整个代码应该做什么:
- 创建代表卡片的对象列表(主牌组)
- 创建一个空列表用于存储已经从主牌组抽取的牌的 ID
- 从主牌组中随机选择 5 张牌并将它们添加到列表(玩家的牌组)
- return 变量的临时整数列表(主牌组中卡片的 ID)
将临时列表中的项目添加到主列表
import random from common import cards deck = cards.deck() # creates a list of 52 class objects representing cards def main(): used_cards_ids = [] # this one is being overwritten in the next step, according to the debugger players_deck, temp_used_cards_ids = generate_players_deck(used_cards_ids) # this is the "next step" used_cards_ids.extend(temp_used_cards_ids) # adds id of the cards which were drawn from the main deck print(used_cards_ids) # prints a double list of cards ids (i.e 1, 2, 3, 4, 5, 1, 2, 3, 4, 5) def generate_players_deck(temp_used_cards_ids): players_deck = [] counter = 0 while counter < 5: # until 5 cards were drawn cards_id = random.randint(0, 51) # chooses a random card if cards_id not in temp_used_cards_ids: # checks if the card with the same id weren't previously drawn counter += 1 temp_used_cards_ids.append(cards_id) # adds card's id to the list of drawn cards players_deck.append(deck[cards_id]) # adds card to the player's deck else: continue return players_deck, temp_used_cards_ids # returns player's deck (list of cards objects) and list of ids of the drawn cards main()
当您将 used_cards_ids
传递给 generate_players_deck()
时,您正在向 used_cards_ids
提供 generate_players_deck()
到 append
数据的主体,您已经别名 到 temp_used_cards_ids
。
然后您从该函数的 return
语句中取回值,您将其存储在变量 temp_used_cards_ids
中,然后 extend
temp_used_cards_ids
,这实际上是 used_cards_ids
,实质上改变了您两次传入的列表。
当您将列表作为函数参数传递时,您传递的不是列表的值,而是传递对原始列表的引用list. 这可能是您感到困惑的地方,或者可能是变量名和函数参数别名之间的切换。