The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
How to print an object variable
Discuss How to print an object variable in the Python Programming forum on Dev Shed. How to print an object variable Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 24th, 2003, 03:19 PM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation 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!
|

July 24th, 2003, 03:51 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

July 24th, 2003, 04:03 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Location: Los Angeles
Posts: 6
Time spent in forums: < 1 sec
Reputation 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.
|

July 24th, 2003, 04:08 PM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation 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?
|

July 24th, 2003, 05:32 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

July 24th, 2003, 07:45 PM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation 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.
|

July 24th, 2003, 08:42 PM
|
|
Junior Member
|
|
Join Date: Jul 2003
Location: Los Angeles
Posts: 6
Time spent in forums: < 1 sec
Reputation 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.
|

July 25th, 2003, 05:47 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

July 25th, 2003, 07:30 AM
|
 |
Contributing User
|
|
Join Date: Jul 2003
Location: France
Posts: 53
  
Time spent in forums: 1 Day 47 m 5 sec
Reputation Power: 13
|
|
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.
|

July 25th, 2003, 07:59 AM
|
|
Registered User
|
|
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18
Time spent in forums: < 1 sec
Reputation 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|