""" A list of class objects to mimic a C type array of structures """ class Person(object): """__init__() functions as the class constructor""" def __init__(self, name=None, job=None, quote=None): self.name = name self.job = job self.quote = quote print # make a list of class Person(s) personList = [] personList.append(Person("Payne N. Diaz", "coach", "Without exception, there is no rule!")) personList.append(Person("Mia Serts", "bicyclist", "If the world didn't suck, we'd all fall off!")) personList.append(Person("Don B. Sanosi", "teacher", "Work real hard while you wait and good things will come to you!")) personList.append(Person("Hugh Jorgan", "organist", "Age is a very high price to pay for maturity.")) personList.append(Person("Herasmus B. Dragon", "dentist", "Enough people can't find work in America!")) personList.append(Person("Adolph Koors", "master-brewer", "Wish you were beer!")) personList.append(Person("Zucker Zahn", "dentist", "If you drink from the fountain of knowledge, quench your thirst slowly.")) print "Mostra un elemento determinato (in questo caso il primo della lista):" print personList[0].name print print "Ordina la lista in base al lavoro..." import operator personList.sort(key=operator.attrgetter('job')) print "... e poi mostra le frasi con l'autore e il lavoro:" for person in personList: print "\"%s\" %s (%s)" % (person.quote, person.name, person.job) print print "Mostra le frasi dei dentisti:" look = 'dentist' for person in personList: if look in person.job: # title() capitalizes the job's first letter print "%s %s: \"%s\"" % (person.job.title(), person.name, person.quote) print print "Cosa ha detto il Sig. Sanosi??" look = "Sanosi" for person in personList: if look in person.name: print "%s: \"%s\"" % (person.name, person.quote)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question