print(object.method) not printing expected output?












0















I'm having a foray into OOP in python,



This project is creating some randomly generated RPG characters



The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.



Here is how the characters are randomly generated:



def generateCharacters():
classes = ["B", "E", "W", "D", "K"]

choice = random.choice(classes)

if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)


and here is the barbarian class, all the other classes are more or less identical:



class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed

# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


I've created a method called getStats, which using string concatenation to make a string that shows all the stats:



# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


When I run the code, it calls main(), which in turn calls menu():



def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)


def main():
gameChars =

for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)

#z = generateCharacters()
menu(gameChars)
#print(z.getStats)


The output I was expecting from the print(x.getStats) would've been, using examples:



Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20


but instead, I get this:



<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>


What am I missing with this? and how could I get the intended output?



Thanks in advance for any help










share|improve this question


















  • 1





    getStats is a function, you have to call it like any other function, so x.getStats()

    – juanpa.arrivillaga
    Nov 21 '18 at 18:22


















0















I'm having a foray into OOP in python,



This project is creating some randomly generated RPG characters



The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.



Here is how the characters are randomly generated:



def generateCharacters():
classes = ["B", "E", "W", "D", "K"]

choice = random.choice(classes)

if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)


and here is the barbarian class, all the other classes are more or less identical:



class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed

# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


I've created a method called getStats, which using string concatenation to make a string that shows all the stats:



# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


When I run the code, it calls main(), which in turn calls menu():



def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)


def main():
gameChars =

for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)

#z = generateCharacters()
menu(gameChars)
#print(z.getStats)


The output I was expecting from the print(x.getStats) would've been, using examples:



Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20


but instead, I get this:



<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>


What am I missing with this? and how could I get the intended output?



Thanks in advance for any help










share|improve this question


















  • 1





    getStats is a function, you have to call it like any other function, so x.getStats()

    – juanpa.arrivillaga
    Nov 21 '18 at 18:22
















0












0








0








I'm having a foray into OOP in python,



This project is creating some randomly generated RPG characters



The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.



Here is how the characters are randomly generated:



def generateCharacters():
classes = ["B", "E", "W", "D", "K"]

choice = random.choice(classes)

if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)


and here is the barbarian class, all the other classes are more or less identical:



class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed

# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


I've created a method called getStats, which using string concatenation to make a string that shows all the stats:



# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


When I run the code, it calls main(), which in turn calls menu():



def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)


def main():
gameChars =

for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)

#z = generateCharacters()
menu(gameChars)
#print(z.getStats)


The output I was expecting from the print(x.getStats) would've been, using examples:



Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20


but instead, I get this:



<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>


What am I missing with this? and how could I get the intended output?



Thanks in advance for any help










share|improve this question














I'm having a foray into OOP in python,



This project is creating some randomly generated RPG characters



The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats.



Here is how the characters are randomly generated:



def generateCharacters():
classes = ["B", "E", "W", "D", "K"]

choice = random.choice(classes)

if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)


and here is the barbarian class, all the other classes are more or less identical:



class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed

# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


I've created a method called getStats, which using string concatenation to make a string that shows all the stats:



# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats


When I run the code, it calls main(), which in turn calls menu():



def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)


def main():
gameChars =

for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)

#z = generateCharacters()
menu(gameChars)
#print(z.getStats)


The output I was expecting from the print(x.getStats) would've been, using examples:



Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20


but instead, I get this:



<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>


What am I missing with this? and how could I get the intended output?



Thanks in advance for any help







python oop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 18:20









LegitShellAccountLegitShellAccount

173




173








  • 1





    getStats is a function, you have to call it like any other function, so x.getStats()

    – juanpa.arrivillaga
    Nov 21 '18 at 18:22
















  • 1





    getStats is a function, you have to call it like any other function, so x.getStats()

    – juanpa.arrivillaga
    Nov 21 '18 at 18:22










1




1





getStats is a function, you have to call it like any other function, so x.getStats()

– juanpa.arrivillaga
Nov 21 '18 at 18:22







getStats is a function, you have to call it like any other function, so x.getStats()

– juanpa.arrivillaga
Nov 21 '18 at 18:22














2 Answers
2






active

oldest

votes


















0














Replace this :



print(x.getStats)


With this :



print(x.getStats())





share|improve this answer
























  • Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)

    – LegitShellAccount
    Nov 21 '18 at 18:35



















0














another version would be to use the @property decorator:



class Barbarian(Character):
@property
def getStats(self):
return 'Name: {.name}'.format(self)


which would allow:



bob = Barbarian('bob', …)
print(bob.getStats)


to work as you expect






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',
    autoActivateHeartbeat: false,
    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%2f53418320%2fprintobject-method-not-printing-expected-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Replace this :



    print(x.getStats)


    With this :



    print(x.getStats())





    share|improve this answer
























    • Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)

      – LegitShellAccount
      Nov 21 '18 at 18:35
















    0














    Replace this :



    print(x.getStats)


    With this :



    print(x.getStats())





    share|improve this answer
























    • Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)

      – LegitShellAccount
      Nov 21 '18 at 18:35














    0












    0








    0







    Replace this :



    print(x.getStats)


    With this :



    print(x.getStats())





    share|improve this answer













    Replace this :



    print(x.getStats)


    With this :



    print(x.getStats())






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 18:24









    Amine MessaoudiAmine Messaoudi

    503514




    503514













    • Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)

      – LegitShellAccount
      Nov 21 '18 at 18:35



















    • Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)

      – LegitShellAccount
      Nov 21 '18 at 18:35

















    Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)

    – LegitShellAccount
    Nov 21 '18 at 18:35





    Ahh yeah... I knew it'd be something simply, I'm an idiot xD ty :)

    – LegitShellAccount
    Nov 21 '18 at 18:35













    0














    another version would be to use the @property decorator:



    class Barbarian(Character):
    @property
    def getStats(self):
    return 'Name: {.name}'.format(self)


    which would allow:



    bob = Barbarian('bob', …)
    print(bob.getStats)


    to work as you expect






    share|improve this answer




























      0














      another version would be to use the @property decorator:



      class Barbarian(Character):
      @property
      def getStats(self):
      return 'Name: {.name}'.format(self)


      which would allow:



      bob = Barbarian('bob', …)
      print(bob.getStats)


      to work as you expect






      share|improve this answer


























        0












        0








        0







        another version would be to use the @property decorator:



        class Barbarian(Character):
        @property
        def getStats(self):
        return 'Name: {.name}'.format(self)


        which would allow:



        bob = Barbarian('bob', …)
        print(bob.getStats)


        to work as you expect






        share|improve this answer













        another version would be to use the @property decorator:



        class Barbarian(Character):
        @property
        def getStats(self):
        return 'Name: {.name}'.format(self)


        which would allow:



        bob = Barbarian('bob', …)
        print(bob.getStats)


        to work as you expect







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 18:35









        Sam MasonSam Mason

        3,16211330




        3,16211330






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418320%2fprintobject-method-not-printing-expected-output%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin