#!/usr/bin/python # -*- encoding: utf-8 -*- __author__ = 'preuss' class Student(object): __anzahl_studenten = 0 # class variables __matrikel_nr = 1 def __init__(self, vorname, nachname=''): self.vorname = vorname # Instance Variable self.nachname = nachname self._gebort = '' # protected => one underscore self.__wohnort = '' # private => intern umbenannt _Student__wohnort Student.__anzahl_studenten += 1 self.matrikel_nr = Student.__matrikel_nr Student.__matrikel_nr += 1 self.courses = [] def get_details(self): return (self.vorname, self.nachname, self.matrikel_nr, ) def delete(self): for c in self.courses: if self in c.course_teilnehmer: c.course_teilnehmer.remove(self) Student.__anzahl_studenten -= 1 def get_anzahl_studenten(self): return Student.__anzahl_studenten def einschreiben(self, course): self.courses.append(course) course.add_participant(self) class Course(object): def __init__(self, course_name): self.course_name = course_name self.course_teilnehmer = [] def add_participant(self, neuer_teilnehmer): self.course_teilnehmer.append(neuer_teilnehmer) def list_participants(self): return [p.get_details() for p in self.course_teilnehmer] # List comprehension participant_list = [] for p in self.course_teilnehmer: participant_list.append(p.get_details()) return participant_list if __name__ == '__main__': s1 = Student('Hilmar', 'Kopper') s2 = Student('Guido', 'Rossum') c1 = Course('Python') c2 = Course('Datenbanken') s1.einschreiben(c1) s1.einschreiben(c2) s2.einschreiben(c2) s1.delete() print(c1.list_participants()) print(c2.list_participants())
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