Misc Python Stuff Flashcards

1
Q

How do you inspect the size of Python objects, in bytes?

A
import sys
sys.getsizeof(object)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s a quick way to reverse a string?

A

my_string[::-1]

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

What are Abstract Base Classes (ABCs)?

A

abc is a builtin module, we have to import ABC and abstractmethod

‘Template’ classes that, when inherited by a class, specify methods and attributes that need to be implemented by that class. E.g.
~~~
from abc import ABC, abstractmethod

class Animal(ABC): # Inherit from ABC(Abstract base class)
@abstractmethod # Decorator to define an abstract method
def feed(self):
pass

class Lion(Animal):
def feed(self):
print(“Feeding a lion with raw meat!”)
~~~

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

T/F: Can ABCs be instantiated?

A

False, they can only be inherited by other classes, not instantiated

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

Write the equation that Python uses to find modulus

A

modulus = value - math.floor(value / base) * base
i.e. subtract the value from the closest lesser value where the base divides evenly

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

Write a test that assures a KeyError is raised when trying to access a non-existent key-value pair in a dictionary

A
def test_should_raise_error_on_missing_key():
    my_dict = dict()
    with pytest.raises(KeyError) as exception_info:
        my_dict["missing_key"]
    assert exception_info.value.args[0] == "missing_key"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When building a class, what order should the static/class methods, public methods, and private methods be presented visually?

A
  1. Static/class methods
  2. Public methods
  3. Private methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is defensive copying, and why is it used when building classes?

A

Defensive copies are shallow copies of the data within the data class presented to the user in place of the actual data. This prevents intentional/unintentional manipulation of the actual data in ways that isn’t allowed within the data structure functionality.

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

What is white-box testing?

A

Testing the internal implementation of your code, instead of the public interface

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

What does !r do in f-string interpolation?

A

It will return the repr() object instead of the string object, giving a more explicit representation

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

What is the purpose of @property when creating a class?

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

What is the purpose of @classmethod when creating a class?

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

How do you get the name of the class from the class’s self?

A

cls = self.\_\_class\_\_.\_\_name\_\_

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