Here's my code :
hp1 = 100
health1 = 'you have', hp1
hp1 = hp1 - 50
health1
print hp1
print health1
This is what it prints :
50
('you have', 100)
Why doesn't the hp1 change inside the health?
Here's my code :
hp1 = 100
health1 = 'you have', hp1
hp1 = hp1 - 50
health1
print hp1
print health1
This is what it prints :
50
('you have', 100)
Why doesn't the hp1 change inside the health?
The following line:
health1 = 'you have', hp1
Is creating a tuple
with two values: "you have"
and 100
(Note that the value of hp1
is copied, and not referenced). It's then assigning this tuple
to a new variable named health1
.
health1
has nothing to do with hp1
. If hp1
get overriden, deleted, thrown away, or anything happens to it, health1
doesn't care.
If you are so eager to pass this variable a reference, you can create a wrapper class around the int
type:
class IntWrapper(object):
def __init__(self, value):
self.value = value
def __add__(self, value):
return IntWrapper(self.value + value)
def __iadd__(self, value):
self.value += value
return self
def __sub__(self, value):
return IntWrapper(self.value - value)
def __isub__(self, value):
self.value -= value
return self
def __str__(self):
return str(self.value)
def __repr__(self):
return str(self)
hp1 = IntWrapper(100)
health1 = 'you have', hp1
hp1 -= 50
print hp1 # 50
print health1 # ('you have', 50)
To automatically change the output with any mutations of hp1
, you can use a class:
class Health:
def __init__(self, health):
self.health = health
def __add__(self, val):
return Health(self.health + val)
def __sub__(self, val):
return Health(self.health - val)
def __repr__(self):
return "you have {}".format(self.health)
hp1 = Health(100)
hp1 -= 50
print(hp1)
Output:
you have 50
Because you defined health1
- a (string, int)
tuple - as hp1
was still 100 and didn't change it since then. This is not a pointer in C/C++ sense, just a copy by value.
In your code you have done like this,
hp1 = 100 # setting hp1 as 100
health1 = 'you have', hp1 # making a tuple
hp1 = hp1 - 50 # subracting 50 from hp1 -> gives 50 as result
health1 # simply calling health1
print hp1 # displaying hp1
print health1 # displaying health1
In this code,
You defined hp1
as 100
, let it be stored in a location 1000
You made a tuple names health1
as 'you have', hp1
. It will be stored in a location say 2000
You subtracted 50 from hp1
making hp1
50, this will make no change to health1
variable because it is stored in different location. But it will change the value of hp1
Hope this helps.!!
To do what you wish to do, you must use a class. This is the closest form of a pointer you will encounter in python.
Here is an example :
class Health():
def __init__(self, value):
self.hp = value
def __repr__(self):
return 'You have {}.'.format(self.hp)
health = Health(100)
hp_clone = health
health.hp -= 50
print hp_clone
# Program outputs : You have 50.
Your question is also a possible duplicate of Pointers in Python? .
What is happening here in your program has been explained by the others.