#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2

    Basic 'class' doubt.


    Here's the code:
    Code:
    class Person:
        def __init__(self, name, job = None, pay = 0):
            self.name = name
            self.job = job
            self.pay = pay
        def lastName(self):
            return self.name.split()[-1]
        def giveRaise(self, percent):
            self.pay = int(self.pay * (1 + percent))
        def __str__(self):
            return '[Person: {0}, {1}]'.format(self.name, self.pay)
    
            
    if __name__ == '__main__':
        bob = Person('Bob Smith') 
        sue = Person('Sue Jones', job='dev', pay=100000)
        print(bob) 
        print(sue)
        print(bob.lastName(), sue.lastName())
        sue.giveRaise(.10)
        print(sue)
    Now, when I run it, I get this result:
    Code:
    [Person: Bob Smith, 0]
    [Person: Sue Jones, 100000]
    Smith Jones
    [Person: Sue Jones, 110000]
    Now, my question is: in the line 20, in the original script
    Code:
    print(bob.lastName(), sue.lastName())
    When it runs, it displays, 'Smith Jones'
    Why is that? I mean, in the class that we coded, we specified and __str__ function which explicitly gave the format to print when we use the print built-in function (i.e., to print the result in brackets). So, why are we not getting the result enclosed in brackets like this:
    Code:
    [Person: Smith Jones]
    ??????????????
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Overloading __str__ changes what happens when you explicitly try to print the class instance itself. That isn't what you are doing here. Here you are printing the return from a class method.

    python Code:
    class Something(object):
        def __str__(self):
            return "What a splendid instance of Something"
        def meth(self):
            return "I'm just a method."
     
    MyGuy = Something()
     
    print(MyGuy)
    print(MyGuy.meth())
    Code:
    >>> 
    What a splendid instance of Something
    I'm just a method.
    >>>
    -Mek
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Mekire
    Overloading __str__ changes what happens when you explicitly try to print the class instance itself. That isn't what you are doing here. Here you are printing the return from a class method.

    python Code:
    class Something(object):
        def __str__(self):
            return "What a splendid instance of Something"
        def meth(self):
            return "I'm just a method."
     
    MyGuy = Something()
     
    print(MyGuy)
    print(MyGuy.meth())
    Code:
    >>> 
    What a splendid instance of Something
    I'm just a method.
    >>>
    -Mek
    OK, thank you soooo much. I really appreciate it.

IMN logo majestic logo threadwatch logo seochat tools logo