Dekorowanie funkcji Hex w celu zerowania zer

Napisałem tę prostą funkcję:

def padded_hex(i, l):
    given_int = i
    given_len = l

    hex_result = hex(given_int)[2:] # remove '0x' from beginning of str
    num_hex_chars = len(hex_result)
    extra_zeros = '0' * (given_len - num_hex_chars) # may not get used..

    return ('0x' + hex_result if num_hex_chars == given_len else
            '?' * given_len if num_hex_chars > given_len else
            '0x' + extra_zeros + hex_result if num_hex_chars < given_len else
            None)

Przykłady:

padded_hex(42,4) # result '0x002a'
hex(15) # result '0xf'
padded_hex(15,1) # result '0xf'

Chociaż jest to dla mnie wystarczająco jasne i pasuje do mojego przypadku użycia (proste narzędzie testowe dla prostej drukarki), nie mogę przestać myśleć, że jest dużo miejsca na ulepszenia, co można sprowadzić do czegoś bardzo zwięzłego.

Jakie są inne podejścia do tego problemu?

questionAnswers(5)

yourAnswerToTheQuestion