OOP in Python Flashcards Preview

INF3331 > OOP in Python > Flashcards

Flashcards in OOP in Python Deck (15)
Loading flashcards...
1

distinguish between local and instance variables

self.x

2

"special features" of Python functions

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

3

private variable

doesn't exist

4

constructor

class MyClass:
def __init__(self, i, j):
self.i = i; self.j = j;
def write(self):
print "i = %d" % (i)

5

declaring and calling instance methods

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

6

obj.someMethod()
class.someMethod(obj)

equal for an instance obj of class class

7

class variables

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

8

get dictionary with all class attributes

Class.__dict__
instance.__class__.__dict__

9

name of class

instance.__class__.__name__

10

list names of all methods and attributes

dir(instance)

11

python functions are also ...

objects, and can therefore be arguments to functions

12

function as argument to function

def apply(func, x, y):
return func(x, y)

13

nested function definitions

def f(x):
def cube(x):
return x*x*x
return cube(x)

14

return function from function

def outer():
def inner():
....
return inner

15

decorate a function

@checkrange
def g(x):
return x**3-2

# or
g = checkrange(g)