How do I change a variable inside a variable?

Hussein Amr

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?

IMCoins

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I change the value of any global variable inside of a function?

From Dev

how can I change values inside a variable?

From Dev

How do I include this variable inside this class?

From Dev

If I make a variable inside a react component, how do I re-access it on a state change?

From Dev

How do I change a scope variable with a service?

From Dev

How do i change a variable of another class?

From Dev

How do I change a Variable, in a Java Class

From Dev

How do I get HTML inside Spacebars {{if}} to change reactively with a Session variable?

From Dev

How exactly do I write a callback function to change a global variable inside AJAX for jquery?

From Dev

How do I change decimal variable to hexadecimal variable in C?

From Dev

How to change a variable inside subroutine?

From Dev

How do i change a variable that i have defined in a different class

From Dev

How do I create a variable cursor inside a store procedure?

From Dev

How do I test a local variable inside a controller with Rspec?

From Dev

How do I append to an existing string variable inside a loop in bash?

From Dev

How do I pass a variable inside an ".each" function?

From Dev

How do I set a public variable inside typescript function?

From Dev

how do I use unix variable inside an HTML file

From Dev

How do I make a variable created inside a function become global?

From Dev

How do I access an variable from a StatefulWidget inside an StatelessWidget?

From Dev

How do I check if an instance class is equal to a variable inside the class?

From Dev

How do I use an array variable inside an array of structures?

From Dev

How do I make a variable created inside a function become global?

From Dev

How do I search for a certain piece of text inside of a variable?

From Dev

How do I expand a variable inside single quotes?

From Dev

How do I update a "global" variable inside a while loop?

From Dev

How do I make a procedure change the value of a variable argument?

From Java

How do I change a variable in each iteration in a list comprehension?

From Dev

How do I change a concrete variable to an existentially quantified var in a hypothesis?

Related Related

  1. 1

    How do I change the value of any global variable inside of a function?

  2. 2

    how can I change values inside a variable?

  3. 3

    How do I include this variable inside this class?

  4. 4

    If I make a variable inside a react component, how do I re-access it on a state change?

  5. 5

    How do I change a scope variable with a service?

  6. 6

    How do i change a variable of another class?

  7. 7

    How do I change a Variable, in a Java Class

  8. 8

    How do I get HTML inside Spacebars {{if}} to change reactively with a Session variable?

  9. 9

    How exactly do I write a callback function to change a global variable inside AJAX for jquery?

  10. 10

    How do I change decimal variable to hexadecimal variable in C?

  11. 11

    How to change a variable inside subroutine?

  12. 12

    How do i change a variable that i have defined in a different class

  13. 13

    How do I create a variable cursor inside a store procedure?

  14. 14

    How do I test a local variable inside a controller with Rspec?

  15. 15

    How do I append to an existing string variable inside a loop in bash?

  16. 16

    How do I pass a variable inside an ".each" function?

  17. 17

    How do I set a public variable inside typescript function?

  18. 18

    how do I use unix variable inside an HTML file

  19. 19

    How do I make a variable created inside a function become global?

  20. 20

    How do I access an variable from a StatefulWidget inside an StatelessWidget?

  21. 21

    How do I check if an instance class is equal to a variable inside the class?

  22. 22

    How do I use an array variable inside an array of structures?

  23. 23

    How do I make a variable created inside a function become global?

  24. 24

    How do I search for a certain piece of text inside of a variable?

  25. 25

    How do I expand a variable inside single quotes?

  26. 26

    How do I update a "global" variable inside a while loop?

  27. 27

    How do I make a procedure change the value of a variable argument?

  28. 28

    How do I change a variable in each iteration in a list comprehension?

  29. 29

    How do I change a concrete variable to an existentially quantified var in a hypothesis?

HotTag

Archive