Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 24th, 2003, 03:19 PM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old July 24th, 2003, 03:51 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #3  
Old July 24th, 2003, 04:03 PM
joebonanno joebonanno is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Los Angeles
Posts: 6 joebonanno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old July 24th, 2003, 04:08 PM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old July 24th, 2003, 05:32 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #6  
Old July 24th, 2003, 07:45 PM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old July 24th, 2003, 08:42 PM
joebonanno joebonanno is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Los Angeles
Posts: 6 joebonanno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old July 25th, 2003, 05:47 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old July 25th, 2003, 07:30 AM
Zoso's Avatar
Zoso Zoso is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: France
Posts: 53 Zoso User rank is Corporal (100 - 500 Reputation Level)Zoso User rank is Corporal (100 - 500 Reputation Level)Zoso User rank is Corporal (100 - 500 Reputation Level)Zoso User rank is Corporal (100 - 500 Reputation Level) 
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.

Reply With Quote
  #10  
Old July 25th, 2003, 07:59 AM
luismg luismg is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Buenos Aires, Argentina
Posts: 18 luismg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to print an object variable

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap