0

I'm attempting to teach myself about super() and class inheritance in python unsuccessfully. Given the following code, can someone tell me why what I expect to happen...isn't?

import random
enemy_list = []

class Entity(object):

    def __init__(self, name=''):
        self.name = name
        self.health = 1
        self.attack_power = .05

class Enemy(Entity):

    def __init__(self, name, target):
        super(Enemy, self).__init__(name)
        self.lvl = random.randint(target.lvl - 2, target.lvl + 2)
        self.health *= self.lvl * target.health
        self.attack_power *= self.lvl

def createEnemy(enemy):
    enemy_list.append(Enemy(enemy, player))
    return enemy_list

enemy_amount = random.randint(1, 5)
while enemy_amount > 0:
    createEnemy(Enemy("goblin", player))
    enemy_amount -= 1

for i in enemy_list:
    print "(", i.lvl, i.name, i.attack_power, i.health, ")"

Why is this code outputting:

( 2 <__main__.Enemy object at 0x7faa040b3050> 0.1 80 )
( 5 <__main__.Enemy object at 0x7faa040b30d0> 0.25 200 )
( 3 <__main__.Enemy object at 0x7faa040b3150> 0.15 120 )
( 5 <__main__.Enemy object at 0x7faa040b31d0> 0.25 200 )

Instead of the expected:

( 2 goblin 0.1 80 )
( 5 goblin 0.25 200 )
( 3 goblin 0.15 120 )
( 5 goblin 0.25 200 )
4
  • what is player, assuming it is an instance of Enemy? Commented Apr 19, 2016 at 20:05
  • @edhurtig player is another instance of Entity, pretty similar code to whats shown in the Enemy class but with different math (no target to base data off of) Commented Apr 19, 2016 at 22:04
  • Hopefully that makes sense, because I've little idea about what I speak of... I'm only using it here to determing the lvl of the enemy and make adjustments to it's attributes depending on that lvl. Commented Apr 19, 2016 at 22:20
  • Makes sense, seems like @Markus got it Commented Apr 19, 2016 at 23:03

3 Answers 3

1

The enemy being passed in on this line:

enemy_list.append(Enemy(enemy, player))

Perhaps you want something like:

def createEnemy(enemy_name):
    enemy_list.append(Enemy(enemy_name, player))
    return enemy_list

while enemy_amount > 0:
    createEnemy("goblin", player)
    enemy_amount -= 1
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Due to your comment, and the comments of others, I was able to figure out I didn't want <code>createEnemy(Enemy("goblin", player))</code> I wanted <code>createEnemy("goblin")</code>. I can't say I understand it any better, but the "flow" of it is starting to make more sense.
1

You're not passing in a string for name, but an enemy object. The order of execution is:

createEnemy(Enemy("goblin", player))

then

enemy_list.append(Enemy(enemy, player))

then

super(Enemy, self).__init__(name)

At this point name is not a string but an enemy object.

1 Comment

Thank you very much. Due to your comment, and the comments of others, I was able to figure out I didn't want <code>createEnemy(Enemy("goblin", player))</code> I wanted <code>createEnemy("goblin")</code>. I can't say I understand it any better, but the "flow" of it is starting to make more sense.
0

You're passing an Enemy object in for the name

def createEnemy(enemy):
    enemy_list.append(Enemy(enemy, player))
    return enemy_list


createEnemy(Enemy("goblin", player))

You call createEnemy, passing in an Enemy. Inside createEnemy, you pass that enemy object as the first argument to create another Enemy (the first argument should be a name).

1 Comment

Thank you very much. Your answer was the one that gave me my moment of clarity, even though it says very similar to the others. Due to your comment I was able to figure out I didn't want <code>createEnemy(Enemy("goblin", player))</code> I wanted <code>createEnemy("goblin")</code>. I can't say I understand it any better, but the "flow" of it is starting to make more sense.

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.