Object-Oriented Python
Everything is an object. Every object has a value. a type. an identity. a namespace. CS105 Python
def initstudent(student, eid): global A class is like a namespace. CS105 Python
def initstudent(student, eid): global A class is like Student a namespace. -- A class definition initstudent binds the class name to an object. These are the active namespaces after the class body. def initstudent(student, eid): CS105 Python 5
a def initstudent(student, eid): student = Student() Invoking a class name creates an instance of that class. -- An instance is like a namespace. These are the active namespaces after executing line a. Student initstudent global is an instance of def initstudent(student, eid): student CS105 Python 6
a b def initstudent(student, eid): student = Student() student.initstudent Accessing an instance s attributes causes chained lookups. These are the active namespaces when executing line b. Student initstudent global 1 is an instance of def initstudent(student, eid): student CS105 Python 7
a b explicit parameter def initstudent(self, eid): self.eid = eid self.courses = [] student = Student() student.initstudent('guido') implicit argument Methods are different from functions. These are the active namespaces just before executing line. Student eid initstudent global local 'guido' eid self is an instance of m def initstudent(student, eid): student CS105 Python 8
def init (self, eid): self.eid = eid self.courses = [] a student = Student('guido') Constructor creates and initializes at the same time. CS105 Python 10
def str (self): return eid Special Method Shortcut init Class() str str(obj) repr (Python representation) eq obj1 == obj getitem obj[i] setitem obj[i] = val Full list at docs.python.org/reference/datamodel.html#specialnames >>> student # calls repr < main.student instance at 0x8cb0> >>> print student # calls str guido CS105 Python 11
5 6 7 8 9 10 11 1 1 1 15 def init (self, eid): self.eid = eid self.courses = [] def enroll(self, course): self.courses.append(course) class Instructor: def init (self, eid): self.eid = eid self.courses = [] def assign(self, course): self.courses.append(course) Inheritance is one way to reuse code. CS105 Python 1
5 6 7 8 9 10 11 1 class Person: def init (self, eid): self.eid = eid self.courses = [] class Student(Person): def enroll(self, course): self.courses.append(course) class Instructor(Person): def assign(self, course): self.courses.append(course) Inheritance is one way to reuse code. CS105 Python 1
5 6 7 8 9 10 11 1 1 1 15 16 class Person: def init (self, eid): self.eid = eid self.courses = [] class Student(Person): def enroll(self, course): self.courses.append(course) class Instructor(Person): def init (self, eid, salary): Person. init (self, eid) self.salary = salary def assign(self, course): self.courses.append(course) Inheritance is one way to reuse code. overriding the base-class method explicitly invoking the base-class method CS105 Python 1
5 6 7 8 a b class Person: class Student(Person): class Instructor(Person): student = Student('guido') instructor = Instructor('wirth', 15000) Class Type >>> type(student) <type 'instance'> >>> type(instructor) <type 'instance'> CS105 Python 15
5 6 7 8 a b class Person(object): class Student(Person): class Instructor(Person): student = Student('guido') instructor = Instructor('wirth', 15000) Class Type >>> type(student) <class ' main.student'> >>> type(instructor) <class ' main.instructor'> CS105 Python 16
Subclassing Built-In Types class ReadOnlyList(list): def setitem (self, index, value): raise TypeError('This is a read-only list!') >>> rol = ReadOnlyList([1,,]) >>> rol[0] 1 >>> rol[0] = 0 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line, in setitem TypeError: This is a read-only list! CS105 Python 17
All a def init (self, eid): self.eid = eid self.courses = [] student = Student('guido') data is public. >>> student.eid 'guido' >>> dir(student) [' doc ', ' init ', ' module ', 'courses', 'eid'] CS105 Python 18
You a def init (self, eid): self. eid = eid self. courses = [] student = Student('guido') can hide it, but >>> student. eid Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Student instance has no attribute ' eid' CS105 Python 19
All a def init (self, eid): self. eid = eid self. courses = [] student = Student('guido') data is public. >>> dir(student) ['_Student courses', '_Student eid', ' doc ', ' init ', ' module '] >>> student._student eid 'guido' CS105 Python 0
Encapsulation By Social Contract signal: _name protect: name The underscore says, This is an implementation detail. Modify at your own peril. CS105 Python 1