How to effectively update the attributes of an object - python

I am creating a "pet game" in order to train my computing skills in python (that is just an excuse: it is because it is fun).
I decided to do a simple RPG game. For that, I defined the class hero:
class hero:
#Common class for the main character
def __init__(self, name, lvl, str, agi, vit, int, luk, prof):
self.name = name
self.lvl = lvl
self.str = str
self.agi = agi
self.vit = vit
self.int = int
self.luk = luk
self.prof = prof
self.exp = 0
if prof==1:
self.dmg=3*(self.str)+1*(self.agi)
self.skillList=['heavySlash01']
self.strUp=3
self.agiUp=1
self.vitUp=2
self.intUp=1
self.lukUp=1
if prof==2:
self.dmg=1*(self.str)+3*(self.agi)
self.skillList=['doubleAttack02']
self.strUp=1
self.agiUp=3
self.vitUp=1
self.intUp=1
self.lukUp=2
if prof==3:
self.dmg=4*(self.int)
self.skillList=['fireBall03']
self.strUp=1
self.agiUp=1.5
self.vitUp=0.5
self.intUp=3.5
self.lukUp=1.5
self.hp=19*vit
However, I noticed that whenever the hero leveled up, I needed to update all of its status separately. For instance, I needed to manually update the hero.dmg. Changing the agi, str and int did not automatically change the dmg as I would expect.
My question is then: Is there a way to make the dmg automatically update itself, based on its formula?

Make dmg a property instead of setting in the __init__ function. The __init__ only runs when the instance is initialized, which is why things aren't updating. However, making it a property runs the method whenever the property is accessed.
#property
def dmg(self):
if prof==1:
return 3*(self.str)+1*(self.agi)
if prof==2:
...

It's better to use inheritance in your case:
class Hero(object):
def __init__(self, name, lvl, _str, agi, vit, _int, luk):
self.name = name
self.lvl = lvl
self._str = _str # Should not use "str" because of reserved keyword of the same name
self.agi = agi
self.vit = vit
self._int = _int # Should not use "int" because of reserved keyword of the same name
self.luk = luk
self.exp = 0
#property
def hp(self):
return 19 * self.vit
class HeroProf_1(Hero):
skillList = ['heavySlash01']
strUp = 3
agiUp = 1
vitUp = 2
intUp = 1
lukUp = 1
#property
def dmg(self):
return 3 * self._str + 1 * self.agi
class HeroProf_2(Hero):
skillList = ['doubleAttack02']
strUp = 1
agiUp = 3
vitUp = 1
intUp = 1
lukUp = 2
#property
def dmg(self):
return 1 * self._str + 3 * self.agi
class HeroProf_3(Hero):
skillList = ['fireBall03']
strUp = 1
agiUp = 1.5
vitUp = 0.5
intUp = 3.5
lukUp = 1.5
#property
def dmg(self):
return 4 * self._int

Related

Python Object initialization,_init_ method

How Can I create class if i have to create object for my class like below.
Obj1 = Class()
Obj2 = Class(para1,para2,para3)
This is related to a task that i need to complete just started learning Python.
I tried construction overloading but it seems to not work in Python .Can anyone tell me how can i achieve this or it is technically wrong to have both line in one code.
You can use *args or **kwargs
class Class1:
def __init__(self, *args):
pass
obj1 = Class1()
obj2 = Class1(para1,para2,para3)
or
class Class1:
def __init__(self, **kwargs):
pass
obj1 = Class1()
obj2 = Class1(para1=para1,para2=para2,para3=para3)
Refer this to learn more about *args and **kwargs
If you set default values like length = 80, you don't have to set them. But if you set the values, it ll be set as you wish. The following code demonstrates almost what you want.
class Rectangle:
def __init__(self, length = 80, breadth = 60, unit_cost=100):
self.length = length
self.breadth = breadth
self.unit_cost = unit_cost
def get_perimeter(self):
return 2 * (self.length + self.breadth)
def get_area(self):
return self.length * self.breadth
def calculate_cost(self):
area = self.get_area()
return area * self.unit_cost
# r = Rectangle() <- this returns with default values
# breadth = 120 cm, length = 160 cm, 1 cm^2 = Rs 2000
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s cm^2" % (r.get_area()))
print("Cost of rectangular field: Rs. %s " %(r.calculate_cost()))

nested object in a class Python

I am obviously missing something fundamental here. Hopefully someone can put me right! TIA
I have an array of objects whose class contains instances of another object. But when I set a property for one of these then they all change.
class direction():
dest = -1
lock = ''
class room():
roomname = ''
desc = ''
n = direction()
s = direction()
w = direction()
e = direction()
item = ''
rooms = []
rooms.append( room() )
rooms.append( room() )
rooms.append( room() )
rooms.append( room() )
rooms.append( room() )
rooms[0].roomname = 'outside'
rooms[0].desc = ''
rooms[0].n.dest = 4
rooms[0].item = ''
rooms[1].roomname = 'hall'
rooms[1].desc = 'The hallway has doors to the east and south'
rooms[1].n.dest = 2
rooms[1].item = ''
if I iterate through the n.dest properties in the rooms list then all are returned as 2
It is as if the direction objects in each object in the rooms list are all a single instance and setting one value in one of them sets it for all of them.
Your attributes are all declared at class level, not instance level, meaning that every instance of the class will share the same values. I think you want:
class Room():
def __init__(self):
self.roomname = ''
self.desc = ''
self.n = direction()
self.s = direction()
self.w = direction()
self.e = direction()
self.item = ''
You're missing constructors, and therefore missing instance variables
You're defining class variables, so each variable is the same between all instances
In each class, you have declared a bunch of class attributes. An assignment like rooms[0].roomname = 'outside' creates an instance attribute that shadows room.roomname. However, you never actually make any such assignment to rooms[0].n, so each assignment to something like rooms[0].n.dest is adding an instance attribute dest to the same instance of direction shared by each instance of room.
In your attempt to "simplify" your code, you've made it more complicated. Define __init__ to set your instance attributes; class attributes are not used as often.
class Direction:
def __init__(self, dest, lock=''):
self.dest = dest
self.lock = lock
class Room:
def __init__(self, roomname, desc, item=''):
self.roomname = roomname
self.desc = desc
self.n = direction()
self.s = direction()
self.w = direction()
self.e = direction()
self.item = item
rooms = []
r = Room('outside', '')
r.n.dest = 4
rooms.append(r)
r = Room('hall', 'The hallway has doors to the east and south')
r.n.dest = 2
rooms.append(r)

Add all variables of instances within class with the same name

I have a class named Robot, which creates a number of instances of the classes Segment and Pointmass. All these instances have a variable mass. How can I obtain the total mass of all the objects within the object with the same variable name mass?
What I do right now:
class Robot:
def __init__(self, massJoint1, massJoint2, massJoint3, massSlide, reachSlide, massArm, lengthArm):
self.joint1 = Pointmass(massJoint1)
self.joint2 = Pointmass(massJoint2)
self.joint3 = Pointmass(massJoint3)
self.slide = Slide(massSlide, reachSlide)
self.arm = Arm(massArm, lengthArm)
self.totalmass = self.joint1.mass + self.joint2.mass + self.joint3.mass + self.slide.mass + self.arm.mass
def printVars(self):
attrs = vars(self)
print(', \n'.join("%s: \t%s" % item for item in attrs.items()))
print()
class Pointmass:
def __init__(self, mass):
self.mass = mass
self.location = None
class Segment:
def __init__(self, mass):
self.mass = mass
self.start = None
self.end = None
In which
self.totalmass = self.joint1.mass + self.joint2.mass + self.joint3.mass + self.slide.mass + self.arm.mass
is a very long line, and is really inconvenient when the Robot gains more arms and joints... Is there a better way to obtain the total mass?
You shouldn't be storing the joints in separate attributes. Rather, store a simple joints list; then you can simply use sum with a generator expression.
self.totalmass = sum(j.mass for j in self.joints)

Python .format() KeyError when using class.__dict__

Basically I have a class defined and I'm trying to display its attributes in a print statement EDIT:
class Player(object):
""" Default Class for the player """
def __init__(self, name):
self.name = name
self.class_type = '[CLASS]'
self.level = 1
self.health = 10
self.maxhealth = self.level * 10
self.attack = 0
self.defence = 0
self.experience = 0
self.weapon = ''
self.shield = ''
self.player_y = 9
self.player_x = 39
print('LV: {level} EXP: {exp} HP: {health}/' +
'{maxhealth}'.format(**char))
Am I doing something wrong? I'm just trying to find a more efficient way to display attributes of a class rather than doing...
print(character.name + ': Weight: ' + character.weight + ' Age: ' +
character.age + '...')
Any ideas?
You've forgotten to use self. in your Player.__init__ function, and you've forgotten to use ** in your call to str.format.
Here is working code:
class Player(object):
def __init__(self, name):
self.name = name
self.age = 125
self.height = 72
self.weight = 154
self.sex = 'Male'
character = Player('NAME')
print('{name} {height} {weight} {sex}'.format(**character.__dict__))

Have a class act as a dict?

I have a class defined like so:
class GameState:
def __init__(self, state=None):
if state is None:
self.fps = 60
self.speed = 1
self.bounciness = 0.9
self.current_level = None
self.next_frame_time = 0
self.init_time = 0
self.real_time = 0
self.game_time = 0
self.game_events = []
self.real_events = []
else:
# THIS being the key line:
self.__dict__.update(**state)
Is there an interface I can define, such that this works (i.e. the ** operator works on my class):
>>> a = GameState()
>>> b = GameState(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: update() argument after ** must be a mapping, not GameState
Essentially, I want b to take on all of the attributes of a.
I didn't think it would work, but I tried defining __getitem__ without any luck.
EDIT: I want to avoid using b's __dict__, as I want to also be able to pass a dictionary as an argument, and potentially use ** on GameState objects elsewhere.
let GameState inherit from dict :
class GameState(dict)
and rewrite the __setattr function like this :
def __setattr__(self,name,value) :
self.__dict__[name] = value
self[name] = value
in order for **obj to work, you have to implement (or inherit) the __getitem__() and keys() methods.
def __getitem__(self, item):
return self.__dict__[item] # you maybe should return a copy
def keys(self):
return self.__dict__.keys() # you could filter those
you could do that by updating the b's dict with that of a when creating b. Try this out:
class GameState:
def __init__(self, state=None):
if state is None:
self.fps = 60
self.speed = 1
self.bounciness = 0.9
self.current_level = None
self.next_frame_time = 0
self.init_time = 0
self.real_time = 0
self.game_time = 0
self.game_events = []
self.real_events = []
else:
if type(state) is dict:
self.__dict__.update(**state)
else:
self.__dict__.update(**state.__dict__)
a = GameState()
b = GameState(a)
you might want to create a deepcopy of the dict because you have a list object as part of the attributes. This is safer as there is no sharing of objects.

Categories