Appending a list using list obtained from a class

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Oct 25 15:06:54 EDT 2012


Demian Brecht wrote:
> On 2012-10-24, at 8:00 AM, inshu chauhan <insideshoes at gmail.com> wrote:
> 
> > Yes, a Class method returns a list. I am trying to append this in main() to make another list.
> > But the list i am getting after appending i showing addresses like this '<__main__.Point object at
> 0x0254FAB0>' but if i print the same in the same loop its showing me numbers which i want. Why I dont know ??
> 
> If you can, please post the relevant blocks of code. That'll be a tremendous help in figuring out your problem.
> 

I would like to re-iterate that posting the relevant code is very
helpful and the best way for us to help you. Everything below is an
educated guess based on the information you provided.


Based on the text '<__main__.Point object at 0x0254FAB0>', it seems
like the object being returned is most likely NOT a list but instead a 
Point class that is iterable. Either that or it is a list of Point 
objects. To find out exactly, try something like this:

lst = # whatever your class is returning.
print 'LIST - type : ', type(lst) '\trepr: ', repr(lst), '\tstr :', str(lst)
for p in lst[-1:]:
    print 'POINT - type: ', type(p) '\trepr: ', repr(p), '\tstr :', str(p)

My suspicion is that when you print the list containing
points/addresses it prints the repr version. When you print the 
points/addresses themselves it prints the str version.

>>> class t(object):
...     def __str__(self):
...         return 'str'
...     def __repr__(self):
...         return 'repr'
...     
>>> a = t()
>>> print [a] 
[repr]
>>> print a
str
>>> print a.__class__.__base__.__repr__(a)
<__pieshell__.t object at 0x11B28F10>

Hope that helps you to understand what is going on.

Ramit Prasad

P.S. Usually it is best to specify, Python version, OS version,
expected input/output, and the smallest code sample you can provide
that shows the problem (that any reader of your message can run).
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list