处理在没有全局变量的函数中使用的变量

时间:2022-08-24 16:15:47

So I'm working on a basic word game where you're dealt a "hand" (a dictionary object) of letters that you use to create words and get points.

因此,我正在开发一个基本的文字游戏,你可以用它来处理一个字母的“手”(字典对象),用来创建单词并获得积分。

This "hand" is used in the parameters of a number of functions: calculating a player's score, updating the number of letters in a hand after a player has used one or more letters, displaying the hand, checking the validity of the player's word, etc.

这个“手”用于许多功能的参数:计算玩家的分数,在玩家使用一个或多个字母后更新手中的字母数,显示手,检查玩家单词的有效性,等等

From all that I've read, I know that I should avoid global variables if I can (though I'm still not totally sure why).

从我读过的所有内容中,我知道如果可以的话我应该避免全局变量(虽然我还不完全确定为什么)。

So what other general approach could I use for a number of functions that use "hand" as a parameter?

那么我可以将其他一般方法用于许多使用“hand”作为参数的函数?

2 个解决方案

#1


9  

It's called an object. Create a class with the shared state, and the functions that share that state.

它被称为一个对象。创建具有共享状态的类以及共享该状态的函数。

The reason why this is better than global variables is that it's a much more restricted version of the same concept - you can clearly see which functions are manipulating those variables, and document (and enforce) the expected invariants on those variables. With global variables it ends up being quite easy to have functions which have different expectations about the state of the shared variables.

这比全局变量更好的原因是它是同一概念的更受限制的版本 - 您可以清楚地看到哪些函数正在操纵这些变量,并且记录(并强制执行)这些变量上的预期不变量。对于全局变量,最终很容易拥有对共享变量状态有不同期望的函数。

It also allows you to have multiple copies of the same object, so instead of having to cast your variables as collections, and correlate between members of the collection, you have a collection of objects, which makes your code simpler. It is then a simple matter to manipulate those objects only through the functions you have defined.

它还允许您拥有同一对象的多个副本,因此您不必将变量转换为集合,而是在集合的成员之间进行关联,而是拥有一组对象,这使您的代码更简单。然后,仅通过您定义的函数来操纵这些对象是一件简单的事情。

#2


8  

Use class encapusulation ... see below ... game is aware of both hands

使用类封套...见下文...游戏意识到双手

class Hand:
    def __init__(self):
       num_cards = 7
       self.cards = ["a" for i in range(num_cards)]

class Game:
    def __init__(self,num_hands=2):
        self.hands = [Hand() for i in range(num_hands)]
        self.current_turn = 0 
    def play(self):
        self.hands[self.current_turn].play()
        self.current_turn = (self.current_turn+1)%len(self.hands)

This is Python 3 code; classes in Python 2 should derive from object.

这是Python 3代码; Python 2中的类应该来自object。

... although they are not required to. if they do not they lose some functionality

......虽然他们不需要。如果他们不这样做,他们会失去一些功能

#1


9  

It's called an object. Create a class with the shared state, and the functions that share that state.

它被称为一个对象。创建具有共享状态的类以及共享该状态的函数。

The reason why this is better than global variables is that it's a much more restricted version of the same concept - you can clearly see which functions are manipulating those variables, and document (and enforce) the expected invariants on those variables. With global variables it ends up being quite easy to have functions which have different expectations about the state of the shared variables.

这比全局变量更好的原因是它是同一概念的更受限制的版本 - 您可以清楚地看到哪些函数正在操纵这些变量,并且记录(并强制执行)这些变量上的预期不变量。对于全局变量,最终很容易拥有对共享变量状态有不同期望的函数。

It also allows you to have multiple copies of the same object, so instead of having to cast your variables as collections, and correlate between members of the collection, you have a collection of objects, which makes your code simpler. It is then a simple matter to manipulate those objects only through the functions you have defined.

它还允许您拥有同一对象的多个副本,因此您不必将变量转换为集合,而是在集合的成员之间进行关联,而是拥有一组对象,这使您的代码更简单。然后,仅通过您定义的函数来操纵这些对象是一件简单的事情。

#2


8  

Use class encapusulation ... see below ... game is aware of both hands

使用类封套...见下文...游戏意识到双手

class Hand:
    def __init__(self):
       num_cards = 7
       self.cards = ["a" for i in range(num_cards)]

class Game:
    def __init__(self,num_hands=2):
        self.hands = [Hand() for i in range(num_hands)]
        self.current_turn = 0 
    def play(self):
        self.hands[self.current_turn].play()
        self.current_turn = (self.current_turn+1)%len(self.hands)

This is Python 3 code; classes in Python 2 should derive from object.

这是Python 3代码; Python 2中的类应该来自object。

... although they are not required to. if they do not they lose some functionality

......虽然他们不需要。如果他们不这样做,他们会失去一些功能