ICS 33 Final Flashcards
24 Questions
100 Views

ICS 33 Final Flashcards

Created by
@WellRegardedObsidian1129

Questions and Answers

How does a class support determining its length?

len(self)

How does a class support determining its truthiness?

  1. 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?

<p>By adding <strong>setitem</strong>(self, index, value) and <strong>delitem</strong>(self, index) methods.</p> Signup and view all the answers

When is an object a sequence?

<p>When a class has both a <strong>len</strong> method and a <strong>getitem</strong> method that accepts non-negative indices.</p> Signup and view all the answers

How does a class support being iterable?

<p>By implementing <strong>iter</strong>(self) and <strong>next</strong>(self) methods.</p> Signup and view all the answers

How does a class support determining whether it holds a specified item?

<p>By implementing the <strong>contains</strong>(self, value) method.</p> Signup and view all the answers

How does a class support being reverse iterable (reversible)?

<p>By implementing the <strong>reversed</strong>(self) method.</p> 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.

<p>subsequence</p> Signup and view all the answers

How to make a sequence?

<p>By having both <strong>len</strong> and <strong>getitem</strong> methods that accept non-negative indices.</p> Signup and view all the answers

How to make an iterable?

<p>By implementing the <strong>iter</strong>(self) method which returns an iterator.</p> Signup and view all the answers

How to make a generator?

<p>If the function has a 'yield' or 'yield from' call.</p> Signup and view all the answers

How to make a context manager?

<p>By implementing <strong>enter</strong>(self) and <strong>exit</strong>(self, exc_type, exc_value, exc_traceback) methods.</p> Signup and view all the answers

How to make a descriptor?

<p><strong>get</strong>(self, instance, owner), <strong>set</strong>(self, instance, value), <strong>delete</strong>(self, instance), <strong>set_name</strong>(self, owner, name)</p> Signup and view all the answers

How does a class support being sliceable?

<p>By making the <strong>getitem</strong>(self, index) method handle slice objects.</p> 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?

<p>By implementing support for <strong>setitem</strong> and <strong>delitem</strong> methods which handle slice objects.</p> Signup and view all the answers

How does a class support being hashable?

<p>By implementing <strong>hash</strong>(self) and <strong>eq</strong>(self, other) methods.</p> Signup and view all the answers

How does equality and inequality work in a class?

<p><strong>eq</strong>(self, other) implements equality checks and <strong>ne</strong>(self, other) implements inequality checks.</p> Signup and view all the answers

How does a class support equality?

<p>By using the <strong>eq</strong>(self, other) dunder method.</p> Signup and view all the answers

How does a class support < and > operations?

<p>By implementing either <strong>lt</strong>(self, other) for &lt; or <strong>gt</strong>(self, other) for &gt; operators.</p> Signup and view all the answers

How to create a table in SQL?

<p>CREATE TABLE person(person_id INTEGER PRIMARY KEY, name TEXT, age INTEGER);</p> Signup and view all the answers

How to ask for all tables created in a SQL database?

<p>SELECT name FROM sqlite_schema;</p> Signup and view all the answers

How to store data in a SQL table?

<p>INSERT INTO person (person_id, name, age) VALUES (1, 'Boo', 13);</p> Signup and view all the answers

How to access data in SQL?

<p>SELECT column1, column2, column3 FROM table WHERE column2 BETWEEN 1 AND 10 ORDER BY length(column1) DESC;</p> 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 or yield from statement.
  • Use __enter__ and __exit__ methods for context management in with 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.

Quiz Team

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!

More Quizzes Like This

Python Classes Overview
8 questions
Use Quizgecko on...
Browser
Browser