5 ms·
Is this enforced in Ruby or just a convention? It's a great convention and is used in Clojure and older Lisps at least with the -p suffix (for predicate). There
by cup-of-tea 9y ago
Is this enforced in Ruby or just a convention? It's a great convention and is used in Clojure and older Lisps at least with the -p suffix (for predicate). There's no reason you can't use the same convention in Python. I've often wanted to write predicates with the -p suffix but stopped myself becasue I don't think other Python people would understand it. Maybe I should just do it anyway.
- masklinn 9y ago> Is this enforced in Ruby or just a convention? It's just conventional.
- fhood 9y agoBut present in basically all the major libraries which is wonderful.
- ralmidani 9y agoIn Ruby, the "?" is optional and used to indicate you are calling a function/method which returns a boolean. The "!" is actually part of the function/method's name. When you want to sort an array, for example, you can either say "sorted_array = array.sort" or you can sort in-place with "array.sort!". So the exclamation distinguishes between the two methods and warns you are performing a destructive operation.
- brudgers 9y ago{being pedantic} In Python, you can't use the '-p' suffix because Python will treat it as subtraction. {being unPythonic} If it makes you happy to use '_p', do it. Explain it in the documentation if it might cause confusion. Be flexible enough that you'll change before letting yourself be voted off the island because it's not that important. {rabble rousing} I think there is room in the world for an alt-python movement...https://github.com/altpython https://github.com/altpython
- cup-of-tea 9y agoYes, you'd have to use just p. The convention in Lisp is it's just p unless the function name is multiple words anyway in which case the words are separated by hyphens and the suffix is -p. I suppose it is more Pythonic to name predicates like "is_full" or something rather than "fullp". But whatever it is I've found I can't stand not using some kind of naming convention for predicates.
- brudgers 9y agoI think the problem is more that Python doesn't really have the idea of predicates as an abstraction. It's not that it doesn't have predicates, it's just that they are not called that and not seen as thing in their own right and not something that programmers are encouraged to write.
- cup-of-tea 9y agoYeah, that's true. Maybe I'm being narrow minded and not "Pythonic" enough, but I always end up writing predicates. I can see that non-trivial predicate functions could be changed to non-trivial functions that return something which can later be compared to get the boolean. But what about having a list of of predicates and wanting to test them all at once. all(p(x) for p in predicates) seems perfect for this...