I am new to python and programming in general, so I will be very grateful for any clarifications on this.
For example, in the following code:
#Using a class class Monster(object): def __init__(self, level, damage, duration): print self self.level = level self.damage = damage self.duration = duration def fight(self): print self print "The monster level is ", self.level print "The monster damage is ", self.damage print "The attack duration is ", self.duration def sleep(self): print "The monster is tired and decides to rest." x = Monster(1, 2, 3) y = Monster(2, 3, 4) x.fight() y.fight() y.sleep() #just using a function def fight_func(level, damage, duration): print "The monster level is ", level print "The monster damage is ", damage print "The attack duration is ", duration fight_func(1, 2, 3) fight_func(5,3,4)
A clean version of the function seems cleaner and gives the same result.
It is the main value of classes that you can create and call many methods on an object, for example. fight or sleep?
function python oop class
user1186742
source share