Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Your function is_digit_char(s) is peculiar. From the name I would expect it to return a boolean, instead it returns a number or None.

To write it like that and have a problem with the falseness of 0 it means that you use it in a way to both use it as conditional expression and an integer value, e.g.

    digit = is_digit_char('0')
    if digit: # fail
        print(digit)
     else:
        print('not a digit')
You should then check for his equality to None

     digit = is_digit_char('0')
     if digit is None: # pass
         print('not a number')
      else:
         print(digit)
But I would argue that you were in search for troubles when you wrote an is_something() function that doesn't return a boolean. That is not idiomatic.

p.s. Hy is too crazy :-)



It's a trivial example and I wouldn't focus on it too much.

It's not that peculiar -- instead of parsing the same character twice you simply return the value that you parsed or None. My inspiration was from the CLHS predicate function, DIGIT-CHAR-P [0].

The real point I was making is that Python has warts that make writing idiomatic code impractical in some situations. I suggest that practicality take precedence over purity. There are some situations that lead to non-idiomatic code and that's okay.

[0] http://clhs.lisp.se/Body/f_digi_1.htm

Update: forgot the link. :)

Update update: Perhaps peculiar to Python because all values of integers are not False except for 0 whereas in another language that doesn't have this wart, anything that isn't False is True... even 0. In other words, anything that isn't False is True. :D


What is a "wart" is subjective. C-like languages also have this "wart" and it's used to great effect, enabling different idioms that would be messier without it.


> In other words, anything that isn't False is True. :D

Except for `nil` (lisps, ruby), and possibly a host of other things depending on the language (empty strings in javascript).


Scheme is the only PL I know that has an explicit #f value.

CL, for all intents and purposes, treats nil as False... but some find the conflation of nil and the empty list runs into the same issue when operating on s-exprs.

    CL-USER> (if '() 1 0)
    0
vs

    scheme> (if '() 1 0)
    1


Logged in to post basically the same comment, then saw you already had. Essentially, no one who knows what they're doing in Python would write the function that way.


It's a trivial example. I've been writing Python since 2.3... maybe I should stop.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: