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

Let's say I have a namedtuple that contains player/score and if I were to replace any of those functions, I would do:

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

How do I create a class that has a function that basically resembles a namedtuple, and is able to replace either player/score manually?

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? 

I'm not sure if I'm making sense here, but if anyone understands my problem I'd much appreciate your feedback. Thanks

1 Answer 1

1

If I get your question right, you need a function which, depending on score's value assigns some new value to it. Is it what you are looking for?

# 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

Example:

my_state = randomState('David', '100')
new_state = my_state.random('100', '200')
Sign up to request clarification or add additional context in comments.

1 Comment

Omg. Thank you so much. I can't believe i didn't think of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.