我是一个
Python新手.我有这个小问题.我想打印一个对象列表,但它打印的是对象的一些奇怪的内部表示.我甚至定义了__str__方法,但我仍然得到这个奇怪的输出.我在这里错过了什么?
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)
请注意,我知道我可以在这里使用for循环或map函数.我正在寻找更短,更惯用的东西.谢谢.
编辑:
还有一个问题:我的上述代码是Pythonic吗?