printing a sequence...

F

Florian Preknya

Hi everybody,

I'm relatively new in using Python and I want to perform a simple job: I
have a list of objects of type A and want to print them to stdout.

class A:
def __init__(self, value):
self.value = value

def __str__(self):
return "value: %d" % self.value

a = [A(10), A(12), A(2)]
print str(a)

I obtain [<__main__.A instance at 0x00767D80>, <__main__.A instance at
0x00768698>, <__main__.A instance at 0x00768350>].
What I want to obtain is [value: 10, value: 12, value: 2].

I know that if I change the __str__ to __repr__ I obtain what I want, but
then I will have these informal strings in debuger also, and I do not want
that.

Can I write somehow the print statement to force calling the A.__str__
function?

Thanks,
Florian.
 
P

Peter Otten

Florian said:
Hi everybody,

I'm relatively new in using Python and I want to perform a simple job: I
have a list of objects of type A and want to print them to stdout.

class A:
def __init__(self, value):
self.value = value

def __str__(self):
return "value: %d" % self.value

a = [A(10), A(12), A(2)]
print str(a)

I obtain [<__main__.A instance at 0x00767D80>, <__main__.A instance at
0x00768698>, <__main__.A instance at 0x00768350>].
What I want to obtain is [value: 10, value: 12, value: 2].

I know that if I change the __str__ to __repr__ I obtain what I want, but
then I will have these informal strings in debuger also, and I do not want
that.

Can I write somehow the print statement to force calling the A.__str__
function?

print map(str, a)

or

print [str(item) for item in a]

Peter
 
P

Peter Otten

Peter said:
Florian Preknya wrote:
What I want to obtain is [value: 10, value: 12, value: 2].
Can I write somehow the print statement to force calling the A.__str__
function?

print map(str, a)

or

print [str(item) for item in a]

Oops, the above will still print the representation of the strings. When you
don't want quotes and escaped non-ascii characters, use

print "[%s]" % ", ".join(map(str, a))

Peter
 
C

Christopher T King

Peter said:
Florian Preknya wrote:
What I want to obtain is [value: 10, value: 12, value: 2].
Can I write somehow the print statement to force calling the A.__str__
function?

print [str(item) for item in a]

print "[%s]" % ", ".join(map(str, a))

I had the same problem with the same solution, and it makes me wonder, why
does a list's __str__() method call the __repr__() method of its members?
Personally, I think the __str__() call should be propagated, not turned
into a __repr__() call. I can't think of any instances in which such
behavior is what you want (if the __repr__() of the members is wanted,
then __repr__() should be used).
 
P

Peter Otten

Christopher said:
Peter said:
Florian Preknya wrote:
What I want to obtain is [value: 10, value: 12, value: 2].
Can I write somehow the print statement to force calling the A.__str__
function?

print [str(item) for item in a]

print "[%s]" % ", ".join(map(str, a))

I had the same problem with the same solution, and it makes me wonder, why
does a list's __str__() method call the __repr__() method of its members?
Personally, I think the __str__() call should be propagated, not turned
into a __repr__() call. I can't think of any instances in which such
behavior is what you want (if the __repr__() of the members is wanted,
then __repr__() should be used).

I didn't find it in the FAQ; I think the argument goes that the results of
propagating str() are sometimes misleading, e. g:
.... def __str__(self):
.... return "[%s]" % ", ".join(map(str, self))
....
List(["a", "b, c", "d"]) ['a', 'b, c', 'd']
print List(["a", "b, c", "d"]) [a, b, c, d]

OK, there may be subtler examples. Personally, I would prefer the above
problem over the current inconsistency.

While I'm at it, here's another alternative for the OP that handles nested
lists nicely:
.... def __init__(self, value):
.... self.value = value
.... def __repr__(self):
.... if isinstance(self.value, list):
.... return str(map(D, self.value))
.... else:
.... return str(self.value)
....
print D(["a", "b", "c"]) [a, b, c]
print D(["a", "b", ["c", "d"]]) [a, b, [c, d]]

Peter
 
L

Lonnie Princehouse

To clarify what's already been said-

thing.__str__() is equivalent to str(thing)

thing.__repr__() is equivalent to repr(thing)


str(some_list) will call repr() for all of the list's items.

You could try:

print map(str, a)

or

print [str(x) for x in a]

for the desired effect, or you could overload __repr__ instead of __str__
 
P

Peter Otten

Lonnie said:
print [str(x) for x in a]

will call repr(str(x)) for all x with undesirable effects for non-ascii
characters and extra quoting.

Looks like you made the same error that I did - after I corrected it :(

Peter
 
D

Dan Bishop

Christopher T King said:
Peter said:
Florian Preknya wrote:
What I want to obtain is [value: 10, value: 12, value: 2].
Can I write somehow the print statement to force calling the A.__str__
function?

print [str(item) for item in a]

print "[%s]" % ", ".join(map(str, a))

I had the same problem with the same solution, and it makes me wonder, why
does a list's __str__() method call the __repr__() method of its members?
Personally, I think the __str__() call should be propagated, not turned
into a __repr__() call. I can't think of any instances in which such
behavior is what you want (if the __repr__() of the members is wanted,
then __repr__() should be used).

seq = ['A, B', 'C']
print seq
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
salsusa

Latest Threads

Top