0

here is the code that I am having a problem with(simplified to make it clearer). It is for a text based game just to help learn things.

class Character(object):
    def __init__(self):
        self.level = 5
        self.moveset = [None,None,None,None] 

    def movesetleveling(self): 
        if self.level > 4:
            self.moveset[0] = Punch(self.level)

def punch(level):
    damagedealt = random.randint(0,5)**level
    return damagedealt

I would like to know how I can make self.moveset[0] = Punch() rather than being equal to the output of Punch() in this block of code. So that everytime i run it in a while loop it will re-evaluate the output of Punch() rather than evaluating Punch() once and assigning that to the 0th index of self.moveset[0].

6
  • 2
    What do you mean by "run it in a while loop"? How are you intending to use moveset? Commented Dec 21, 2013 at 6:28
  • Do you want to have four random punch levels in self.moveset or do you want the first item in self.moveset to be a random punch value in each iteration of your loop? Commented Dec 21, 2013 at 6:30
  • @BrenBarn moveset will be used to show a selection of player moves. they will select a move from the list which will then be evaluated to produce an integer value which will be a damagestat. this damagestat will affect a Foes health. I have created a while loop which does this over and over but each time it uses the same value for self.moveset[0] rather than getting a new value. Commented Dec 21, 2013 at 6:36
  • Add your while loop to the question Commented Dec 21, 2013 at 6:41
  • @BurhanKhalid i want the first item to be a random punch value in each iteration of the loop. Commented Dec 21, 2013 at 6:43

1 Answer 1

2

You could assign a function to self.moveset[0] instead of its result (using functools.partial()):

from functools import partial

self.moveset[0] = partial(punch, self.level)

Then later, in your while loop, just call it:

while True:
    new_punch_every_time = self.moveset[0]() #note: parentheses

self.moveset[0]() calls punch function with level parameter set to self.level (its value at the time of partial call).

It works because functions, methods are first class citizens in Python. You can pass them as parameters to other functions, return from functions, bind to a different name, append to a list, etc.

Sign up to request clarification or add additional context in comments.

7 Comments

partial? to a beginner? ;)
@thefourtheye: OP explicitly said: "to help learn things". functools.partial() is the thing to learn. It is not a crocodile; it won't eat you if you come near it.
Nice, but I suspect the problem might be in the while loop (of the original question)
@J.F.Sebastian just had a look at the functools docs and i somewhat agree with @thefourtheye 's sentiment. my head is swimming. A slightly more beginner friendly answer would be greatly appreciated if you know of one!
@user3124704: which concept/word do you not understand in this sentence: "self.moveset[0]() calls punch function with level parameter set to self.level (its value at the time of the partial call)."? Do you know what function is? What is function parameter? What does it mean to call a function with a given parameter?
|

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.