Questions and Answers
How does a class support determining its length?
len(self)
How does a class support determining its truthiness?
- If the object has a bool(self) method, its result is used to determine the object's truthiness. 2. Otherwise, if the object has a len(self) method, its result is used instead — with zero being falsy and anything non-zero being truthy. 3. Otherwise, the object is always considered to be truthy.
How does a class support indexing?
By adding at least one dunder method to their class: getitem(self, index)
If a class supports indexing, how can it support assigning into an index and deletion of an index?
Signup and view all the answers
When is an object a sequence?
Signup and view all the answers
How does a class support being iterable?
Signup and view all the answers
How does a class support determining whether it holds a specified item?
Signup and view all the answers
How does a class support being reverse iterable (reversible)?
Signup and view all the answers
Slicing allows us to take a sequence of objects and obtain a _________, containing some of the objects while skipping others.
Signup and view all the answers
How to make a sequence?
Signup and view all the answers
How to make an iterable?
Signup and view all the answers
How to make a generator?
Signup and view all the answers
How to make a context manager?
Signup and view all the answers
How to make a descriptor?
Signup and view all the answers
How does a class support being sliceable?
Signup and view all the answers
If a class supports indexing and slicing, how can it support assigning into a slice and deletion of a slice?
Signup and view all the answers
How does a class support being hashable?
Signup and view all the answers
How does equality and inequality work in a class?
Signup and view all the answers
How does a class support equality?
Signup and view all the answers
How does a class support < and > operations?
Signup and view all the answers
How to create a table in SQL?
Signup and view all the answers
How to ask for all tables created in a SQL database?
Signup and view all the answers
How to store data in a SQL table?
Signup and view all the answers
How to access data in SQL?
Signup and view all the answers
Study Notes
Class Method Functions
- Use
__len__(self)
to define an object's length. - Determine truthiness with
__bool__(self)
or fallback to__len__(self)
; zero is falsy, non-zero is truthy.
Indexing Support
- Implement
__getitem__(self, index)
to return the value for a specified index; index can be any type as determined by the method. - Custom classes can raise exceptions for invalid indices with descriptive messages.
Assigning and Deleting by Index
- Support assigning into an index with
__setitem__(self, index, value)
. - Implement deletion via
__delitem__(self, index)
.
Sequence Definition
- A class is considered a sequence if it has both
__len__
and__getitem__
methods using non-negative indices.
Iterability Support
- Make a class iterable by defining
__iter__(self)
and__next__(self)
methods. - Alternatively, sequences can be iterable via
__len__
and__getitem__
.
Membership & Reversibility
- Use
__contains__(self, value)
to check if a value exists in a sequence. - Implement
__reversed__(self)
for reverse iteration.
Slicing Mechanics
- Use slicing to extract a subsequence with
object[start:stop:step]
.
Making a Sequence
- Objects with both
__len__
and__getitem__
automatically support the sequence protocol and thus are iterable.
Generators and Context Managers
- A function turns into a generator with a
yield
oryield from
statement. - Use
__enter__
and__exit__
methods for context management inwith
statements.
Descriptor Protocol
- Define descriptors with
__get__
,__set__
,__delete__
, and__set_name__
.
Slicing in Classes
- Implement
__getitem__(self, index)
to handle slice objects for support of slicing.
Hashability in Classes
- An object is hashable if it has
__hash__(self)
and__eq__(self, other)
methods. - Hash functions must ensure equivalent objects have the same hash.
Equality Operations
- Customize equality checks using
__eq__(self, other)
and implement inequality with__ne__(self, other)
.
Comparisons
- Implement
__lt__
,__gt__
,__le__
, and__ge__
for supporting comparative operations.
Positional and Keyword Arguments
- Use
*args
and**kwargs
for unpacking positional and keyword arguments in a function. - Indicate positional-only or keyword-only parameters using
*
or/
in the function signature.
SQL Table Creation & Data Manipulation
- Create tables in SQL with
CREATE TABLE
. - Retrieve all tables with
SELECT name FROM sqlite_schema
. - Store data securely using parameterized queries.
- Query data with specific conditions and order using
SELECT
,WHERE
,ORDER BY
.
Data Integrity
- Ensure that equality and inequality operations behave correctly with respect to hash values in classes.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge of class behaviors in Python with these final flashcards. Learn how classes determine their length and truthiness using special methods. Perfect for preparing for the ICS 33 course final exam!