diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:55:07 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:56:55 +0200 |
| commit | d98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch) | |
| tree | 267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /mit/extented_class_example/MITPerson.py | |
| parent | 869e68986aa8f69af6e7842260a68d1e5c6f796f (diff) | |
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'mit/extented_class_example/MITPerson.py')
| -rw-r--r-- | mit/extented_class_example/MITPerson.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/mit/extented_class_example/MITPerson.py b/mit/extented_class_example/MITPerson.py new file mode 100644 index 0000000..b6bc856 --- /dev/null +++ b/mit/extented_class_example/MITPerson.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3 + +from Person import * + +class MITPerson(Person): + + nextIdNum = 0 # next ID number to assign + + def __init__(self, name): + Person.__init__(self, name) # Initialize person's attributes + self.idNum = MITPerson.nextIdNum # MITPerson global attribute: unique ID + MITPerson.nextIdNum += 1 + + def getIdNum(self): + return self.idNum + + # Sorting MIT people uses their ID number, not name + def __lt__(self, other): + return self.idNum < other.idNum + + def speak(self, utterance): + return (self.name + " says: " + utterance) + +class Student(MITPerson): + pass + +class UG(Student): + def __init__(self, name, classYear): + MITPerson.__init__(self, name) + self.year = classYear + + def getClass(self): + return self.year + + def speak(self, utterance): + return MITPerson.speak(self, " Yo Bro: " + utterance) + +class Grad(Student): + pass + +class TransferStudent(Student): + pass + + + +# By creating a new 'blank' class Student, and making all UG, Grad and +# TransferStudent classes inherit from it, we clean up the class inheritance a +# bit, so, in the function below, we can simply us Student class, to check if an +# object belongs to that class, instead of the old version (commented out below, +# for historical perspective) +def isStudent(obj): +# return isinstance(obj, UG) or isinstance(obj, Grad) + return isinstance(obj, Student) + +class Professor(MITPerson): + def __init__(self, name, department): + MITPerson__init__(self, name) + self.department = department + + def speak(self, utterance): + new = "In course " + self.department + " we say " + return MITPerson.speak(self, new + utterance) + + def lecture(self, topic): + return self.speak('it is obvious that ' + topic) + +m1 = MITPerson("Mark Zuckeberg") +m2 = MITPerson("Bill Gates") +m3 = MITPerson("Carlos Maiolino") +p1 = Person("Drew Houston") + + +s1 = UG("Matt Damon", 2017) +s2 = Grad("Leonardo di Caprio") +# Remember, comparison is done by using __lt__ method, which takes the instance +# itself + another instance. When using the MITPerson instance, the __lt__ +# method uses the idNum attribute, which, an instance of Person class will not +# have. But, using an instance of Person class, the __lt__ method, uses the +# LastName, which, since MITPerson is a subclass of Person class, it also +# contains a lastName attribute + +## print(p1 < m3) # Comparison works +## print(m3 < p1) # Comparison does not work (Attribute error) |
