class AnonObject(dict):
__getattr__ = dict.get
__setattr__ = dict.__setitem__
class Person:
def __init__(self, name, age, hobby):
self.hobby = hobby
self.age = age
self.name = name
def __repr__(self):
return "{0} is {1} and likes {2}".format(self.name, self.age, self.hobby)
people = [
Person("Jeff", 50, "Biking"),
Person("Michael", 40, "Motocross"),
Person("Sarah", 30, "Running"),
Person("Tony", 24, "Jogging"),
Person("Zoe", 12, "TV")
]
# Query
bikers = [
AnonObject(Name=person.name, PastTime=person.hobby) ü select result into anonymous type
for person in people
if person.hobby == "Biking"
]
bikers.sort(key=lambda p: p.Name)
for biker in bikers:
print(biker)