Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

teach_cs_toronto_edu_csc148h_notes_inheritance_python_special_methods_html

CSC148 Notes

Uploaded by

racayow849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

teach_cs_toronto_edu_csc148h_notes_inheritance_python_special_methods_html

CSC148 Notes

Uploaded by

racayow849
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

CSC148 Course Notes 3.

9 The object Class and Python Special


CSC148 Course Notes Methods
1. Recapping and Extending Some Now that we understand inheritance, we can gain a deeper understanding of some of Python’s fundamental special methods.
Key Prerequisite Material

1.1 The Python Memory Model:


Introduction The object superclass
1.2 The Python Memory Model: In our very first lecture, we described every piece of data as an object, and have continued to use this term throughout this
Functions and Parameters
course. It turns out that “object” is not merely a theoretical concept, but made explicit in the Python language. In Python, we
1.3 The Function Design Recipe have a class called object , which is an ancestor of every other class, both built-in classes like int or our user-defined
1.4 Preconditions classes like Employee .[1]
1.5 Python Type Annotations

2. Testing Your Code Inheriting special methods


2.1 Testing Your Work
This object class gives default implementations for many special methods we have seen before, including:
2.2 Choosing Test Cases
__init__ , which allows us to create instances of a class even when the class body is empty—it’s not magic, our classes
2.3 Code Coverage
simply inherit object.__init__ ! So every time we define __init__ within our own class, we are actually overriding
2.4 Introduction to Property-Based
object.__init__ .
Testing
__str__ , which is called when you call either str or print on an object. The default implementation? Identifying the

3. Object-Oriented Programming class name and a location in your computer’s memory:

3.1 Introduction to Object-Oriented


>>> class Donut:
Programming
... pass
3.2 Representation Invariants ...
>>> d1 = Donut()
3.3 The Class Design Recipe >>> print(d1)
<Donut object at 0x103359828>
3.4 More on Designing Classes

3.5 Inheritance: Introduction and


__eq__ , which is called when you use == to compare objects. The default object.__eq__ implementation simply uses
Methods
is to compare the two objects.
3.6 Inheritance: Attributes and
Initializers
>>> donut1 = Donut()
3.7 Inheritance: Tracing Initialization >>> donut2 = Donut()
>>> donut1 == donut2 # compares `donut1 is donut2`, which is False
3.8 Inheritance: Thoughts on Design False
>>> donut1 == donut1 # compares `donut1 is donut1`, which is True
3.9 The object Class and Python True
Special Methods

Keep in mind that even though these methods are called “special”, overriding them in your classes works in the exact same
4. Abstract Data Types
way as other methods: simply define a method with the specific name of that special method.
4.1 Introduction to Abstract Data
Types
[1] By “ancestor” we mean either a parent class, or a parent of a parent class, etc.
4.2 Stacks and Queues

4.3 Exceptions Previous Next


 3.8 Inheritance: Thoughts on Design 4.1 Introduction to Abstract Data Types

4.4 Analysing Program Running
Time
By Diane Horton and David Liu

5. Exceptions © Copyright 2022.

You might also like