Can't change value of class











up vote
1
down vote

favorite












I have the following Python code:



class Characters():
default_player_hp = 100.0
default_enemy_hp = 100.0
class Player:
def __init__(self):
self.player_health = Characters().default_player_hp
def update(self):
self.player_health = Characters().default_player_hp
class Enemy:
def __init__(self):
self.enemy_health = Characters().default_enemy_hp
def update(self):
self.enemy_health = Characters().default_enemy_hp
characters = Characters()
player = characters.Player()
while (True):
characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)
enemy = characters.Enemy()
enemy.update()
print(characters.default_enemy_hp, enemy.enemy_health)


The problem is it doesn't change enemy_hp variable (always prints [default_enemy_hp] 100.0).










share|improve this question


















  • 3




    Why are the classes nested, anyway?
    – timgeb
    Nov 21 at 18:54










  • Because why not? Anyway, it doesn't change anything. I've tried.
    – Shiney
    Nov 21 at 18:58






  • 1




    I think you're confusing class variables and instance variables. Try reading digitalocean.com/community/tutorials/…
    – Fred Larson
    Nov 21 at 19:21















up vote
1
down vote

favorite












I have the following Python code:



class Characters():
default_player_hp = 100.0
default_enemy_hp = 100.0
class Player:
def __init__(self):
self.player_health = Characters().default_player_hp
def update(self):
self.player_health = Characters().default_player_hp
class Enemy:
def __init__(self):
self.enemy_health = Characters().default_enemy_hp
def update(self):
self.enemy_health = Characters().default_enemy_hp
characters = Characters()
player = characters.Player()
while (True):
characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)
enemy = characters.Enemy()
enemy.update()
print(characters.default_enemy_hp, enemy.enemy_health)


The problem is it doesn't change enemy_hp variable (always prints [default_enemy_hp] 100.0).










share|improve this question


















  • 3




    Why are the classes nested, anyway?
    – timgeb
    Nov 21 at 18:54










  • Because why not? Anyway, it doesn't change anything. I've tried.
    – Shiney
    Nov 21 at 18:58






  • 1




    I think you're confusing class variables and instance variables. Try reading digitalocean.com/community/tutorials/…
    – Fred Larson
    Nov 21 at 19:21













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have the following Python code:



class Characters():
default_player_hp = 100.0
default_enemy_hp = 100.0
class Player:
def __init__(self):
self.player_health = Characters().default_player_hp
def update(self):
self.player_health = Characters().default_player_hp
class Enemy:
def __init__(self):
self.enemy_health = Characters().default_enemy_hp
def update(self):
self.enemy_health = Characters().default_enemy_hp
characters = Characters()
player = characters.Player()
while (True):
characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)
enemy = characters.Enemy()
enemy.update()
print(characters.default_enemy_hp, enemy.enemy_health)


The problem is it doesn't change enemy_hp variable (always prints [default_enemy_hp] 100.0).










share|improve this question













I have the following Python code:



class Characters():
default_player_hp = 100.0
default_enemy_hp = 100.0
class Player:
def __init__(self):
self.player_health = Characters().default_player_hp
def update(self):
self.player_health = Characters().default_player_hp
class Enemy:
def __init__(self):
self.enemy_health = Characters().default_enemy_hp
def update(self):
self.enemy_health = Characters().default_enemy_hp
characters = Characters()
player = characters.Player()
while (True):
characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)
enemy = characters.Enemy()
enemy.update()
print(characters.default_enemy_hp, enemy.enemy_health)


The problem is it doesn't change enemy_hp variable (always prints [default_enemy_hp] 100.0).







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 18:51









Shiney

63




63








  • 3




    Why are the classes nested, anyway?
    – timgeb
    Nov 21 at 18:54










  • Because why not? Anyway, it doesn't change anything. I've tried.
    – Shiney
    Nov 21 at 18:58






  • 1




    I think you're confusing class variables and instance variables. Try reading digitalocean.com/community/tutorials/…
    – Fred Larson
    Nov 21 at 19:21














  • 3




    Why are the classes nested, anyway?
    – timgeb
    Nov 21 at 18:54










  • Because why not? Anyway, it doesn't change anything. I've tried.
    – Shiney
    Nov 21 at 18:58






  • 1




    I think you're confusing class variables and instance variables. Try reading digitalocean.com/community/tutorials/…
    – Fred Larson
    Nov 21 at 19:21








3




3




Why are the classes nested, anyway?
– timgeb
Nov 21 at 18:54




Why are the classes nested, anyway?
– timgeb
Nov 21 at 18:54












Because why not? Anyway, it doesn't change anything. I've tried.
– Shiney
Nov 21 at 18:58




Because why not? Anyway, it doesn't change anything. I've tried.
– Shiney
Nov 21 at 18:58




1




1




I think you're confusing class variables and instance variables. Try reading digitalocean.com/community/tutorials/…
– Fred Larson
Nov 21 at 19:21




I think you're confusing class variables and instance variables. Try reading digitalocean.com/community/tutorials/…
– Fred Larson
Nov 21 at 19:21












1 Answer
1






active

oldest

votes

















up vote
0
down vote













This snippet:



def update(self):
self.enemy_health = Characters().default_enemy_hp


makes a new (anonymous) Character object, and sets the current enemy_health attribute of the given Enemy object to 100. That's because new Character's always start with 100 health.



This:



characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)


is therefore irrelevant, as enemy.update() will create a new Character object (it will not use the Character object modified above), and just use that new Character object's health (i.e. 100).



If you want to actually update the health, don't work so hard, and just pass in a value and be done with it:



def update(self, new_health):
self.enemy_health = new_health


Usage:



enemy.update(new_health=15) # or new_health=round(float(input()) / 10, 3), whatever you want.
print(enemy.enemy_health) # outputs 15, as desired


HTH.



P.S. You've got other issues with your code, mostly having to do with creating new Character objects (similar problems to the update issue you've asked about), but this answer only focuses on why update is not working as expected.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418790%2fcant-change-value-of-class%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This snippet:



    def update(self):
    self.enemy_health = Characters().default_enemy_hp


    makes a new (anonymous) Character object, and sets the current enemy_health attribute of the given Enemy object to 100. That's because new Character's always start with 100 health.



    This:



    characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)


    is therefore irrelevant, as enemy.update() will create a new Character object (it will not use the Character object modified above), and just use that new Character object's health (i.e. 100).



    If you want to actually update the health, don't work so hard, and just pass in a value and be done with it:



    def update(self, new_health):
    self.enemy_health = new_health


    Usage:



    enemy.update(new_health=15) # or new_health=round(float(input()) / 10, 3), whatever you want.
    print(enemy.enemy_health) # outputs 15, as desired


    HTH.



    P.S. You've got other issues with your code, mostly having to do with creating new Character objects (similar problems to the update issue you've asked about), but this answer only focuses on why update is not working as expected.






    share|improve this answer

























      up vote
      0
      down vote













      This snippet:



      def update(self):
      self.enemy_health = Characters().default_enemy_hp


      makes a new (anonymous) Character object, and sets the current enemy_health attribute of the given Enemy object to 100. That's because new Character's always start with 100 health.



      This:



      characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)


      is therefore irrelevant, as enemy.update() will create a new Character object (it will not use the Character object modified above), and just use that new Character object's health (i.e. 100).



      If you want to actually update the health, don't work so hard, and just pass in a value and be done with it:



      def update(self, new_health):
      self.enemy_health = new_health


      Usage:



      enemy.update(new_health=15) # or new_health=round(float(input()) / 10, 3), whatever you want.
      print(enemy.enemy_health) # outputs 15, as desired


      HTH.



      P.S. You've got other issues with your code, mostly having to do with creating new Character objects (similar problems to the update issue you've asked about), but this answer only focuses on why update is not working as expected.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This snippet:



        def update(self):
        self.enemy_health = Characters().default_enemy_hp


        makes a new (anonymous) Character object, and sets the current enemy_health attribute of the given Enemy object to 100. That's because new Character's always start with 100 health.



        This:



        characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)


        is therefore irrelevant, as enemy.update() will create a new Character object (it will not use the Character object modified above), and just use that new Character object's health (i.e. 100).



        If you want to actually update the health, don't work so hard, and just pass in a value and be done with it:



        def update(self, new_health):
        self.enemy_health = new_health


        Usage:



        enemy.update(new_health=15) # or new_health=round(float(input()) / 10, 3), whatever you want.
        print(enemy.enemy_health) # outputs 15, as desired


        HTH.



        P.S. You've got other issues with your code, mostly having to do with creating new Character objects (similar problems to the update issue you've asked about), but this answer only focuses on why update is not working as expected.






        share|improve this answer












        This snippet:



        def update(self):
        self.enemy_health = Characters().default_enemy_hp


        makes a new (anonymous) Character object, and sets the current enemy_health attribute of the given Enemy object to 100. That's because new Character's always start with 100 health.



        This:



        characters.default_enemy_hp = round(characters.default_enemy_hp * float(input()) / 10, 3)


        is therefore irrelevant, as enemy.update() will create a new Character object (it will not use the Character object modified above), and just use that new Character object's health (i.e. 100).



        If you want to actually update the health, don't work so hard, and just pass in a value and be done with it:



        def update(self, new_health):
        self.enemy_health = new_health


        Usage:



        enemy.update(new_health=15) # or new_health=round(float(input()) / 10, 3), whatever you want.
        print(enemy.enemy_health) # outputs 15, as desired


        HTH.



        P.S. You've got other issues with your code, mostly having to do with creating new Character objects (similar problems to the update issue you've asked about), but this answer only focuses on why update is not working as expected.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 19:20









        Matt Messersmith

        5,64521729




        5,64521729






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418790%2fcant-change-value-of-class%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Trompette piccolo

            Slow SSRS Report in dynamic grouping and multiple parameters

            Simon Yates (cyclisme)