Protocols Flashcards

1
Q

__bool__()

A

class Rectangle:

def __init__(self, w, h):

self. width = w
self. height = h

def __bool__(self):

if self.width and self.height:

return True

else:

return False

>>> bool(Rectangle(10, 15))

True

>>> bool(Rectangle(0, 1))

False

Also, if an object supplies a __len__() method, python will consider False objects of zero length.

and, or and not will now use this method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

mathematical operators

A
  1. __add__(self, value) can be accessed with +
  2. __sub__(self, value) obvious
  3. __mul__(self, factor) obvious
  4. __truediv__(self, divisor) is ‘/’ - ie. 5/4 = 1.25
  5. __floordiv__(self, divisor) is ‘//’ - ie. 5/4 = 1
  6. __mod__(self, divisor) is ‘%’ - ie. 5/4 = 0.25
  7. __divmod__(self, divisor) is divmod() and returns a tuple, the floordiv and the modulo
  8. __pow__(self, power, modulo = None) uses ‘**’, <modulo></modulo>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Bitwise operations

A

Not terribly important to me

  1. __lshift__() __rshift__() shift bits on to left or right
  2. & AND, __and__()
  3. | or __or__() [one or both]
  4. ^ __xor__() [strictly one]
  5. ~ __invert__() [?] takes a number and returns the opposite, sorta.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

operation variations

A

There is an operator, each has a left hand method, right hand method, and inline method.

right hand is the left hand prepended with ‘r’

__add__() becomes __radd__()

inline adds an “i”

__add__() becomes __iadd__()

division and modulo don’t have an inline method.

Bitwise inversion doesn’t have a right hand or inline method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Numbers

A
  1. __index__(self) determines how a class will index itself
  2. round(3.14, 1) = 3.1
  3. round(3.14) = 3
  4. round(3.14, 0) = 3.0

import decimal

round(decimal.Decimal(‘3.14’), 1) == Decimal(‘3.1’)

round(decimal.Decimal(‘3.14’)) == 3

round(256, -1) == 260

round(512, -2) == 500

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Sign Operations

A
  1. __neg__()
  2. __pos__() doesn’t really do anything
  3. __abs__()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Comparison operators

A
  1. __eq__()
  2. __ne__() - if you use __eq__, define this as well
  3. __lt__() and __lte__()
  4. __gt__() and __gte__()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Iterables

A

an object is iterable if passing it to the built-in iter() returns an iterator. iter() first inspects the object, looking for an __iter__() method

two things required

  1. __iter__() - not all that interesting. typically it just returns self and usually doesn’t need defining
  2. __next__() - where all the real work happens.

if Python sees something that stops a loop while the iterator still has values in it, the iterator remains with those values in it. This happens in break, return, or raise

raise StopIteration is what finally stops it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Repeatable Generators

A

now

def repeatable(generator):
   """a decorator to turn a generator into an   object that can be iterated multiple times"""
   class RepeatableGenerator:
     def \_\_init\_\_(self, \*args, \*\*kwargs):
     self.args = args
     self.kwargs = kwargs
   def \_\_iter\_\_(self):
      return iter(generator(\*self.args, \*\*self.kwargs))
   return RepeatableGenerator

@repeatable

def gen(max):

__for x in range(max):

____yield x

etc.

Problems with this example:

  1. give it a function, returns a class
  2. can’t be used as a method b/c has no __get__() method
  3. wrap function in functools.wraps

@functools.wraps(generator)

def wrapper(*args, **kwargs):

__return RepeatableGenerator(*args, __**kwargs)

**return wrapper **

instead of RepeatableGenerator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

__getitem__()

__setitem__()

A

__getitem__():

  1. receives an index or slice object
  2. raises IndexError if index is out of range

__setitem__():

  1. also accepts index or slice, but also values or a value
  2. it is expected to remove item(s) from the index or slice and replace them with the new items, expanding or contracting the list of new items as necessary.

append and insert add items.

__setitem__ does not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

__contains__()

__delitem__()

remove()

A

del keyword is just like remove. Items after the deleted one are moved left. It calls the __delitem__() method rather than the explicit remove() method call.

in is the keyword that calls __contains__(). Contains accepts an object and returns True or False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Mappings

A

keys to mappings may be any hashable object:

  1. dates
  2. times
  3. strings
  4. integers
  5. tuples

slice objects are not hashable, so they can’t be keys

mapping methods

  1. keys(). __iter__ goes over the keys, should be defined if making custom mapping
  2. values() goes over the values
  3. items() iterates over k,v pairs in tuples
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

__call__()

A

makes an object callable. Anything can be callable

as a function, it can be decorated any number of times, but any decorator needs to account for the instance being passed as the first argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

context managers

A

context managers are used in a with statement

Allows an object to define what it means to work within the context of that object - ex. setup and clean up.

two distinct steps:

**__enter__() : **

receives no additional arguments, just the instance. provides initialization of code block in with statement

if with statement includes an as clause, __enter__() returns a value to that variable

__exit__():

it’s job is to return the context to whatever it was prior to the with statement. In the case of files, it closes it. this is still called even if return, yield, continue, or break statements end the block

exit gets three arguments to determine if the block stopped normally or there was an exception. class of the exception, the instance of that class, and traceback object. Any implementation of exit must accept these three arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly