How to (and should I) avoid repetition while assigning values from different sources? In a Mako template partial I accept an argument (step) that can be an instance of Step class, a dict or None. Can I in some way avoid repetition or do the check in other more 'pythonic' way? Or how could I gracefully avoid checking types at all?

&lt;%include file="_step.mako" args="step=step" /&gt;


and then in _step.mako:

&lt;%page args="step"/&gt;
&lt;%! from package.models.recipe import Step %&gt;
&lt;% 
if isinstance(step, Step):
    number = step.number
    time_value = step.time_value
    text = step.text
elif type(step) is dict:
    number = step['number']
    time_value = step['time_value']
    text = step['text']
else:
    number = 1
    time_value = ''
    text = ''
%&gt;
1.I assume if type(step) is dict: should be elif type(step) is dict:.

2.isinstance is preferable than type because isinstance caters for inheritance, while checking for equality of type does not (it demands identity of types and rejects instances of subtypes).

3.You also can apply duck-typing. 

try:
    number = step.number
except AttributeError:
    try:
        number = step['number']
    except KeyError:
        number = 1


And these ways are quite pythonic.

4.I suggest you can inherit your Step class from dict then you don't need to check types. Yo will just do

number = step.get('number', 1)
time_value = step.get('time_value', '')