0
randomState = collections.namedtuple('randomState', ['player', 'score'])

プレーヤー/スコアを含む名前付きタプルがあり、それらの関数のいずれかを置き換える場合は、次のようにします。

def random(state):
    return state._replace(score='123')

基本的に名前付きタプルに似た関数を持ち、いずれかのプレーヤー/スコアを手動で置き換えることができるクラスを作成するにはどうすればよいですか?

class Random:
    def abc(self, score):
        if self.score == '123':
            ###had it were a namedtuple, I would use the replace function here, but classes don't allow me to do that so how would I replace 'score' manually? 

ここで意味があるかどうかはわかりませんが、誰かが私の問題を理解している場合は、フィードバックをいただければ幸いです。ありがとう

4

1 に答える 1

1

質問が正しければ、スコアの値に応じて新しい値を割り当てる関数が必要です。それはあなたが探しているものですか?

# recommended to use object as ancestor
class randomState(object):
    def __init__(self, player, score):
        self.player = player
        self.score = score

    def random(self, opt_arg1, opt_arg2):
        # you may want to customize what you compare score to
        if self.score == opt_arg1:
            # you may also want to customize what it replaces score with
            self.score = opt_arg2

例:

my_state = randomState('David', '100')
new_state = my_state.random('100', '200')
于 2013-03-02T10:49:41.320 に答える