PyWart: Language missing maximum constant of numeric types!

Wolfgang Meiners WolfgangMeiners01 at web.de
Sun Feb 26 14:16:24 CET 2012


Am 25.02.12 21:35, schrieb Rick Johnson:
> On Feb 25, 11:54 am, MRAB <pyt... at mrabarnett.plus.com> wrote:
>> [...]
>> That should be:
>> if maxlength is not None and len(string) <= maxlength:
> 
> Using "imaginary" infinity values defiles the intuitive nature of your
> code. What is more intuitive?
> 
> def confine_length(string, maxlength=INFINITY):
>     if string.length < maxlength:
>         do_something()
> 
> def confine_length(string, maxlength=None):
>     if maxlength is not None and len(string) <= maxlength:
>         do_something()

I just had a closer look at it. It seems to be more complicated than i
thougth: You will have to write

def confine_length(string, maxlength=None):
    if maxlength: # maxlength exists, comparison possible
        if len(string) <= maxlength:
            do_something()
    else: # maxlength does not exist, so always do something
        do_something()

you migth also write

def confine_length(str, maxlength=None):
    do_it = (len(str) <= maxlength) if maxlength else True
    if do_it:
        do_something()

but it really does not look intuitive. Hmm. My idea was that None is a
perfect Value for infinity since there is no infinitely large number.
But as i see it, you must have two comparisons then. Maybe someone has a
better idea?

Wolfgang



More information about the Python-list mailing list