OOP in Python Flashcards

1
Q

distinguish between local and instance variables

A

self.x

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

“special features” of Python functions

A

functions are regular objects; passed as arguments, returned from function, modified inside functions etc.

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

private variable

A

doesn’t exist

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

constructor

A
class MyClass:
    def \_\_init\_\_(self, i, j):
        self.i = i; self.j = j;
    def write(self):
        print "i = %d" % (i)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

declaring and calling instance methods

A

all instance methods take self as first argument in the declaration, but no in the call

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

obj. someMethod()

class. someMethod(obj)

A

equal for an instance obj of class class

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

class variables

A

class variables are common to the class;

>>>class Point(object):
    counter = 0
    def \_\_init\_\_(self):
        Point.counter +=1
>>> p0 = Point()
>>> p1 = Point()
>>> Point.counter
2
>>> p0.counter
2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

get dictionary with all class attributes

A

Class.__dict__

instance.__class__.__dict__

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

name of class

A

instance.__class__.__name__

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

list names of all methods and attributes

A

dir(instance)

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

python functions are also …

A

objects, and can therefore be arguments to functions

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

function as argument to function

A
def apply(func, x, y):
    return func(x, y)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

nested function definitions

A
def f(x):
    def cube(x):
        return x*x*x
    return cube(x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

return function from function

A
def outer():
    def inner():
        ....
    return inner
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

decorate a function

A
@checkrange
def g(x):
    return x**3-2
# or
g = checkrange(g)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly