Quick Python Question

Started by
21 comments, last by Zahlman 17 years, 6 months ago
Why don't you create objects and explicitly pass them around? That way you can audit access/ownership.
Advertisement
Quote:Original post by Emmanuel77
Why don't you use a global class with all your useful vars ?

Because that's horrible design? If you're working with an object-oriented language, your system should be modular. Each class should only know about the objects that it needs. Global variables = evil. Singletons used as a substitute for global variables = evil.
If anyone could link to information/tutorial on objects that would be great. I don't understand what they are.

Anyways, new Question.
class Wolf:    Name = 'Wolf'    Hp = 50    Str = 10    Agil = 40    G = 2    Xp = 1    def Attack( ):        print "The Wolf attacks dealing", Wolf.Str , "damage"        Stat.Hp = Stat.Hp - Wolf.Str	        print "Your Hp now is", Stat.Hp def arena( ):    move = 0    victory = 0    ranval = random.randint(0, 10)    if ranval <= 7:        En = Wolf    print "You are in the Colosseum fightning a", En.Name    Eny.Hp = En.Hp    while move != 3:        if En.Agil > Stat.Agil:            En.Attack( )        print "[1] = Attack"        print "[2] = Spell/Ability"        print "[3] = Flee"        move = input("Choose your move")        if move == 1:            print "You attack the", En.Name , "dealing", Stat.Str , "damage"             Eny.Hp = Eny.Hp - Stat.Str            if Eny.Hp < 0:                Eny.Hp = 0            print "The ", En.Name , "health is", Eny.Hp        if En.Agil <= Agil:            En.Attack( )        if Eny.hp <= 0:            move = 3            print "You killed the" , En.Name            print "You gained", En.G , "Gold"            Stat.G = Stat.G +En.G            print "You gained", En.Xp , "Xp"            Stat.Xp = Stat.Xp + En.Xp


What I am trying to do here with classes, is in the Arena function have En change depending on who your up against. Currently, I want each instance of En to be "Wolf" so that when it says En.Name, the program prints out Wolf, which is from Wolf.Name.

This works fine, except for
En.Attack( )

The error I get is this:
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)

What I think that means is that the program wants me to use Wolf.Attack( ) instead of En.Attack. I would really much rather not do this.

So what should I do?
Function whithin a class must have self as argument. So it should be

def Attack(self):
Programming is easy, just copy/paste tutorials...
Thanks

Arg, I changed the Def Attack( ): to Def Attack(self):
and I changed the En.Attack( ) to En.Attack(self)
Now I get
File "/Users/chris/Desktop/NewGame.py", line 103, in arena
En.Attack(self)
NameError: global name 'self' is not defined
Quote:
Thanks

Arg, I changed the Def Attack( ): to Def Attack(self):
and I changed the En.Attack( ) to En.Attack(self)
Now I get
File "/Users/chris/Desktop/NewGame.py", line 103, in arena
En.Attack(self)
NameError: global name 'self' is not defined


When you call the method, don't provide the self argument. It is already the 'En' object that you call the method on.

define :
def Attack( self ):

And call
En.Attack()


Emmanuel
It sounds like you need to read Dive Into Python. Get through the objects section at least.
Quote:Original post by drakostar
It sounds like you need to read Dive Into Python. Get through the objects section at least.


Thx
Gunna go through the whole thing.
I cant for the life of me get a function inside a class to work.
Heres the error I get.
File "/Projects/NewGame.py", line 108, in EnemyCheck
Wolf.Attack( )
TypeError: unbound method Attack() must be called with Wolf instance as first argument (got nothing instead)

def EnemyCheck( ):
global EnemyName
if EnemyName == Wolf.Name:
Wolf.Attack( )
Sounds like Wolf is a class, not an object. You are trying to call a method without providing an object. This is probably closer to what you want:
w = Wolf()w.Attack()


Although, if you have an "EnemyName", I assume you also have an "Enemy" ... which should be an object (possibly of type 'Wolf') in the first place.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement