Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Printing dictionary values rather than references

Reply
Thread Tools

Printing dictionary values rather than references

 
 
Amit Dor-Shifer
Guest
Posts: n/a
 
      06-10-2009
Hi all.

I'd like to print-out a dictionary of objects. The printed values are
references. How Do I print the actual objects.

class MyClass:
def __str__(self):
return str(self.__dict__)

if __name__ == '__main__':
dict = dict()
classA = MyClass()
setattr(classA, "attr-1", "val-1")

dict['a']= classA
print classA
''' Desired output: {'attr-1': 'val-1'}'''
print dict
''' Actual output: {'a': <__main__.MyClass instance at 0x79cfc8>}'''

Thanks,
Amit
 
Reply With Quote
 
 
 
 
Jeff McNeil
Guest
Posts: n/a
 
      06-10-2009
On Jun 10, 10:19*am, Amit Dor-Shifer <(E-Mail Removed)> wrote:
> Hi all.
>
> I'd like to print-out a dictionary of objects. The printed values are
> references. How Do I print the actual objects.
>
> class MyClass:
> * * def __str__(self):
> * * * * return str(self.__dict__)
>
> if __name__ == '__main__':
> * * dict = dict()
> * * classA = MyClass()
> * * setattr(classA, "attr-1", "val-1")
>
> * * dict['a']= classA
> * * print classA
> * * ''' Desired output: {'attr-1': 'val-1'}'''
> * * print dict
> * * ''' Actual output: {'a': <__main__.MyClass instance at 0x79cfc8>}'''
>
> Thanks,
> Amit


class MyClass:
def __repr__(self): # <--- see http://docs.python.org/library/functions..html#repr
return str(self.__dict__)
HTH,

Jeff
mcjeff.blogspot.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing results in array rather than printing joseph85750@yahoo.com Perl Misc 6 02-03-2007 01:36 AM
Databindings property - I want to disable true values rather than false values Steve ASP .Net 4 01-27-2006 01:02 PM
Re: Private LAN: why should the gateway address be at the low end of the range, rather than at the high end. Ted Jones Cisco 11 11-04-2005 05:56 AM
Newbie: Program printing result to DOS window, rather than a newscreen Irish Rover Java 2 12-13-2004 08:26 AM
Main Advantages i get when i do a stand alone app in .Net rather than in VB. Punya Narra ASP .Net 5 02-17-2004 12:02 PM



Advertisments