Python Flashcards

(13 cards)

1
Q

What is a dunder method?

A

A special method or magic method, internal to python framework objects that begin and end with a double underscore:

collection.__getitem__(key)

This is the dunder method getitem.

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

What is a namedtuple?

A

A class of object that is a bundle of attributes with no custom methods, like a database record.

Card = collections.namedtuple(‘Card’, [‘rank’,’suit’])

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

Which module contains namedtuple?

A

collections

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

How do you make a class respond to the len() function?

A

Implement the __len__ dunder method:

def __len__(self):

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

How do you make a class indexable (my_class[0])?

A

Implement the __getitem__ dunder method:

def __getitem__(self, position):

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

How do you get a random item from a sequence?

A

Use the choice method in the random module:

from random import choice

choice( sequence )

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

What dunder method returns the string representation of an object?

A

def __repr__(self):

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

If you can only implement __repr__ or __str__ which one would you implement and why?

A

You would implement __repr__, because if there is no custom __str__ available Python will call __repr__ as a fallback.

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

What should the output of __repr__ be?

A

The output should be unambiguous and, if possible, match the source code necessary to re-create the object being represented.

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

What is the difference between the types of things contained by a container sequence and a flat sequence?

A

A container sequence contains references to the objects within. A flat sequence physically stores the value of each item within its own memory space and not as distinct objects.

Flat sequences are more compact, but limited to holding primitives such as characters, bytes, and numbers.

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

What is a list comprehension for producing a cartesian product of two lists?

A

[(x,y) for x in list1 for y in list2]

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

What is the main difference in memory utilization between a list comprehension and a generator expression?

A

A list comprehension generates a complete list in memory. A generator expression generates elements one at a time (so has better memory performance in a for loop, for example).

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

How to reverse a string s?

A

’‘.join(reversed(s))

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