|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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? |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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.
|
|
#8
|
||||
|
||||
|
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. |
|
#9
|
||||
|
||||
|
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. |
|
#10
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to print an object variable |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|