tags:

views:

131

answers:

4

Hi fellow stackies.

I am a Python newbie. I have this small problem. I want to print a list of objects but all it prints is some weird internal representation of object. I have even defined __str__ method but still I am getting this weird output. What am I missing here?

class person(object):
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def __str__(self):
     return self.name + "(" + str(self.age) + ")"

def partition(coll, pred):
  left = []
  right = []
  for c in coll:
    if pred(c):
      left.append(c)
    else:
      right.append(c)
  return left, right


people = [
  person("Cheryl", 20),
  person("Shemoor", 14 ),
  person("Kimbala", 25),
  person("Sakharam", 8)
]

young_fellas, old_fellas = partition(people, lambda p : p.age < 18)
print(young_fellas)
print(old_fellas)

Please note that I know I can use either a for loop or a map function here. I am looking for something shorter and more idiomatic. Thanks.

EDIT:

One more question: Is the above code of mine Pythonic?

+7  A: 

Unless you're explicitly converting to a str, it's the __repr__ method that's used to render your objects.

See Difference between __str__ and __repr__ in Python for more details.

Blair Conrad
Thank you! Though you'll still have to wait for 8 minutes for the green cookie. :-)
one-zero-zero-one
A: 

try overriding repr

  def __repr__(self):
      return self.name + "(" + str(self.age) + ")"

Edit: Better way, Thanks to Paul.

  def __repr__(self):
     return "%s(%d)" % (self.name, self.age)
st0le
String interpolation `"%s(%d)" % (self.name, self.age)` or `''.join(list_of_string_bits)` are better styles to learn than adding up little bits of strings using '+'.
Paul McGuire
@st0le and @Paul: Thank you for the suggestion :-)
one-zero-zero-one
@Paul McGuire: in this particular case not really, so don't troll it :)
Nas Banov
+1  A: 

You could do:

print(map(str,young_fellas))
DiggyF
You should read the question completely you know.
one-zero-zero-one
About the pythonic part, there's a section on that in the docs. You could use the repr(lib) to create your own debug representation. In this case person("name",1) is a better representation than person(name,1) because it should look like a valid Python expression that could be used to recreate an object.http://docs.python.org/reference/datamodel.html#object.__repr__http://www.python.org/dev/peps/pep-0008/
DiggyF
Thank you for the suggestion :-)
one-zero-zero-one
+2  A: 

Your made this object:

person("Cheryl", 20)

This means repr should be same after creation:

def __repr__(self):
 return 'person(%r,%r)' % (self.name,self.age)

Output becomes:

[person('Shemoor',14), person('Sakharam',8)]
[person('Cheryl',20), person('Kimbala',25)]
Tony Veijalainen
Thank you for the suggestion :-)
one-zero-zero-one
You are welcome. Nice function you have written, this partition. I am a string partition fan myself and I dig it.
Tony Veijalainen
++: Comes with the bonus that the output can directly be fed back into python (what the idea of repr() is anyway)
Nas Banov