I would do something like:
class Switch(object):
def __init__(self, on=True):
self.on = on
self.off_word = 'off'
def turn_off(self, key):
self.on = key!=self.off_word
def __str__(self):
return "Switch(on=%s)" % self.on
myswitch = Switch()
while myswitch.on:
myswitch.turn_off(input('Enter a word: '))
print(myswitch)
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Line two does not make sense, because of line 4 and it also makes instanses' methods strangely available as methods' attribute. Also the line 4 should not work then if you need the original hp value. It is in most cases not what you actually need to do to use attributes of class like in line 5. Usually the attributes should be defined in __init__ method of the class to catch this kind of errors and then you can have more than one instance of class. Mostly you are also not returning values in methods as the results are updated attributes. Your code should have lots of references of self.something or parameter.something not Class.something. Try to understand the way I did in the code I posted in response to your earlier code in the other thread.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
"self" is just a reference to the class, so that the class knows that the function or variable is in it's name space, so you have to include it when calling a function of the class from/within the class, otherwise the compiler will default to looking outside of the class for the function or variable.
class OffSwitch(object):
def __init__(self):
self.keycheck='off'
print self.keycheck
self.turn_off('yellow')
def turn_off(self,key):
self.keycheck = key
print self.keycheck
OS = OffSwitch()
#
# outside the class
OS.turn_off("outside")
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
This would fix it, but I would rather go with a formal instance ...
class OffSwitch(object):
def __init__(self):
self.keycheck='off'
def turnOff(self,key):
if key==self.keycheck:
SystemExit
else:
print('will continue')
switchword=input('Enter a word: ')
OffSwitch().turnOff(switchword)
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
A class does not exist, it is a prototype for actual objects. Only a class instance exists. That is why we declare an instance for each class object desired.
class Person(object):
def __init__(self, name, health=100):
self.name=name
self.health=100
def hurt(self, amount):
print("%s health = %d - %d" % (self.name, self.health, amount))
self.health-=amount
class Killer(object):
def __init__(self):
import random
power= random.randint(10, 21)
self.power=power
print("power:", self.power)
def stab(self,Person):
Person.hurt(self.power)
K = Killer()
James=Person('James')
Karen=Person('Karen')
K.stab(James)
K.stab(Karen)
K.stab(James)
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
import random
class Person(object):
def __init__(self, name, health=100):
self.name = name
self.health = 100
def hurt(self, amount):
self.health -= amount
print self.health
def __str__(self):
return repr('My name is {name} and my health is {health}'.format(name=self.name, health=self.health))
class Killer(Person):
def __init__(self, name, health=100):
Person.__init__(self,name, health)
power = random.randint(10, 21)
self.power = power
def stab(self,reciever):
reciever.hurt(self.power)
if __name__ == '__main__':
stabee = Person('James')
staber = Killer('Henry')
print stabee
print staber
staber.stab(stabee)
print stabee
print staber
Here's my take on it.
Killer inherits from Person, as a Killer is a type of Person.
The problem you were getting was from using an instance of a class.
Killer.stab(James)
This doesn't do what you think it does. It has no instance to work with, so there is no "self" to play with. It doesn't create an instance, so the methods can't be called without supplying an instance to work on.
Killer().stab(James)
On the other hand creates an instance of Killer, albeit a throw-away, and uses method stab.
This is show when I use dis -
def a():
Killer().stab(James)
def b():
Killer.stab(James)
dis.dis(a)
dis.dis(b)
Which gives
60 0 LOAD_GLOBAL 0 (Killer)
<strong>3 CALL_FUNCTION 0</strong>
6 LOAD_ATTR 1 (stab)
9 LOAD_GLOBAL 2 (James)
12 CALL_FUNCTION 1
15 POP_TOP
16 LOAD_CONST 0 (None)
19 RETURN_VALUE
63 0 LOAD_GLOBAL 0 (Killer)
3 LOAD_ATTR 1 (stab)
6 LOAD_GLOBAL 2 (James)
9 CALL_FUNCTION 1
12 POP_TOP
13 LOAD_CONST 0 (None)
16 RETURN_VALUE
I've highlighted the important line.
HTH
Enalicho
Junior Poster in Training
62 posts since Aug 2011
Reputation Points: 28
Solved Threads: 13