python - Using an __init__ argument in other functions of the same class -



python - Using an __init__ argument in other functions of the same class -

so i'm trying design class weapon scheme in text based game. basically, want swords deal 1-6 harm , add together harm based on weapons modifier. got class work if phone call function in class modifier, want set class 1 time , not have type multiple times

this code have.

import dice #uses random generation simulate dice rolls class sword(): def __init__(self, name, modifier): self.name = name self.mod = modifier def dmg(self, modifier): base of operations = dice.d6 self.dmg = base of operations + modifier homecoming self.dmg x = sword("sword of coolness", 1) print x.name print x.dmg(1)

that's why did self.mod = modifier. value available methods self.mod. can create method:

def dmg(self): base of operations = dice.d6 self.dmg = base of operations + self.mod homecoming self.dmg

as ys-l notes, in comment, though, don't want that, since you're overwriting dmg method value. maybe improve thought give method different name:

def modify_dmg(self): base of operations = dice.d6 self.dmg = base of operations + self.mod homecoming self.dmg

python

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -