
Deep Idea: “Duck Typing”

Duck Typing is a computer programming concept to determine whether 
an object can be used for a particular purpose. 

It is applying the duck test:

    If it looks like a duck, swims like a duck, and quacks like a duck, 
    then it probably is a duck.


# Example: Filenames versus Iterables

# WHich is better ? (1)

# Provide filename
def read_data(filename):
    records = []
    with open(filename) as f:
        for line in f:
            ...
            records.append(r)
    return records

d = read_data('file.csv')



# WHich is better ? (1)

# Provide lines
def read_data(lines):
    records = []
    for line in lines:
        ...
        records.append(r)
    return records

with open('file.csv') as f:
    d = read_data(f)


Option (2) is better, because it can be used with OTHER iterables:

# A CSV file
lines = open('data.csv')
data = read_data(lines)

# A zipped file
lines = gzip.open('data.csv.gz','rt')
data = read_data(lines)

# The Standard Input
lines = sys.stdin
data = read_data(lines)

# A list of strings
lines = ['ACME,50,91.1','IBM,75,123.45', ... ]
data = read_data(lines)

