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

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep Power
    0

    How to print an object variable


    Hi,
    I'm new to python and programing in general and, after playing with the language for a couple of days, I always get into the same problem. For example:

    >>> luis=math(1,5,7)
    >>> jon=math(4,8,7)
    >>> for i in list:
    print i, i.calc()


    <__main__.math instance at 0x013038B0> 4
    <__main__.math instance at 0x01309B60> 6
    >>>

    I created two instances of object math (which gets qualifications for math students). The instances are added to a list, and then, a for loop is used to calculate the average qualification.

    However, what I get instead of "i" is this strange description of the instance, instead of its name (for example "luis" or "jon").

    Why? How can I do it so I get "luis" or "jon" instead of "<__main__.math instance...>" ??

    I hope I made myself clear...sorry for my english.
    Any help would be highly appreciated. Thanks!
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hi luis,

    Little comfusion here, could you post the whole session so we can see what exactly your doing? You generally get output like that when you try to print an object (functions, class etc.) with no value.

    Take care,
    Mark.
    Last edited by netytan; July 24th, 2003 at 04:02 PM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Los Angeles
    Posts
    6
    Rep Power
    0
    I think the problem is that you need to average the math scores within the for loop. Just add the calculation before you call print.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep Power
    0

    complete code


    Well, this is the code for the classes:

    list=[]

    class math:
    def __init__(self, term, exam, tesis):
    self.term = term
    self.exam = exam
    self.tesis = tesis
    list.append(self)
    def calc(self):
    return (self.term + self.exam + self.tesis)/3

    class science(math):
    def __init__(self, term, exam, tesis, pract):
    math.__init__(self, term, exam, tesis)
    self.pract = pract
    def calc(self):
    return (self.term + self.exam + self.tesis + self.pract)/4

    This are just two classes, one for math students and another for science students. What the 'def calc' does is calculate the average grade for each student.
    So in my previous email, I created two instances (luis and jon), and I used a loop for calculating both qualifications.

    I wanted to get this output:

    luis 4
    jon 6

    but instead I got:

    <__main__.math instance at 0x013038B0> 4
    <__main__.math instance at 0x01309B60> 6

    In sume, I wanted to display the name of the object, the instance (luis or jon). I don't want to display "<__main__:math inst...>".
    You know what I mean?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The instance is appended to the list when you append 'self' in the math class. Self doesnt contain the name of the variable inisalizing the object, the best way to do this would be to add a name option to you class and append this to list. This should work.

    Code:
    list=[] 
    
    class math: 
    	def __init__(self, name, term, exam, tesis):
    		self.term = term 
    		self.exam = exam 
    		self.tesis = tesis 
    		list.append([self ,name]) 
    	def calc(self): 
    		return (self.term + self.exam + self.tesis)/3 
    
    class science(math): 
    	def __init__(self, name, term, exam, tesis, pract): 
    		math.__init__(self, name, term, exam, tesis) 
    		self.pract = pract 
    	def calc(self): 
    		return (self.term + self.exam + self.tesis + self.pract)/4
    
    math('jon',4,8,7)
    math('tom',4,8,7) 
    
    for i in list: 
    	print i[1], i[0].calc()
    
    raw_input()
    Anyway hope this does what you want.

    Have fun,
    Mark.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep Power
    0
    Thanks Mark,
    It's curious, I had already though about this way before, but I wasn't satisfied because I was sure that I was doing something wrong...
    And now I learned that there's no need to give a 'name' to each new instance!

    I used to do it this way: newInstance=math('Jon', 8, 9, 4.5)

    but you just did it this way: math('Jon', 8, 9, 4.5)

    I just never imagined that I can do that!

    Well, thank you for your time...
    Thanks to you too, Joe!

    Luis

    p.d: By the way: if you don't name the object, how do you access its properties??
    for example: newInstance.name (newInstance is the name)
    but if you create the object without giving it a name:
    math('jon', 8,9,4)
    How do you access its property??
    Last edited by luismg; July 24th, 2003 at 09:23 PM.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Los Angeles
    Posts
    6
    Rep Power
    0
    No need to thank me just for trying. I miss read your first question and did not know the answer once you clarified it. New to Python myself.
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Very welcome Luis, glad to help. Joe, you gave your input which is afterall what a comunity does. Its the thought that counts

    have fun guyz,
    Mark.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    France
    Posts
    53
    Rep Power
    15
    There's already a function that allows you to print an object : it's the __repr__ method.

    Here is an example :

    Code:
    class Human :
            def __init__(self, firstname, lastname) :
                    self.firstname = firstname
                    self.lastname = lastname
    
            def __repr__(self) :
                    return "%s %s" % (self.firstname, self.lastname)
    And here is what you obtain when using it :

    Code:
    >>> human1 = Human("John", "Smith")
    >>> print human1
    'John Smith'
    In fact, when you type "print obj", Python searches for the __repr__ method of the object.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Location
    Buenos Aires, Argentina
    Posts
    18
    Rep Power
    0

    Good one but...


    this is not exactly what I need...
    I'll try to explain with an example (this is very simple, nothing to do with real banking...):

    For this class:

    class BankAccount:
    def __init__(self, initialAmount):
    self.balance = initialAmount
    print "Account created with balance %5.2f" % self.balance
    list.append(self)

    def printit(self):
    print self.balance

    I create accounts through a menu option as follows:

    if menu_choice == 1:
    name = raw_input('Enter applicants name: ')
    amount = input('Enter initial deposit: ')
    name=BankAccount(amount)

    The problem is that when I try to access an instance property, such as: anyname.printit(), I get an error like this one:

    Traceback (most recent call last):
    File "C:\WINDOWS\Escritorio\pypy.py", line 103, in ?
    x.printit()
    AttributeError: 'str' object has no attribute 'printit'

    The same happens if I type in IDE: anyname.balance

    However, it doesn't happen if I create the instance statically, by typing in the IDE: anyname=BankAccount(initialamount). It only happens when I create the instances through the above menu option.
    Last edited by luismg; July 25th, 2003 at 08:03 AM.

IMN logo majestic logo threadwatch logo seochat tools logo