On Tuesday 28 October 2003 07:10 pm, Michael Chermside wrote: > Alex Martelli writes: > > BTW, I think I should point out one POSSIBLE problem with > > classmethods -- since unfortunately they CAN be called on an > > instance, and will ignore that instance, this may confuse an > > unsuspecting user. > > Alex, that's a good point, and one we should be careful of. Thanks, that's why I brought the issue up. > However, (as you said) I suspect that the unsuspecting users > will always call it with zero arguments. So long as that call > always fails (preferably with a useful error message) I think > we should be OK. > > So what if we make the error message maximally useful? Something *VERY good idea* > like this: > > _privateObj= Object() > def sorted(iteratorToSort=_privateObj): > if iteratorToSort == _privateObj: > raise TypeError('sorted is a classmethod of list ' + > 'taking an iterator argument') > else: > <... normal body here ...> > > The only thing I've done here was to make the text of the message > more helpful (I've even left the type of the exception as TypeError > even though that might not be the most useful thing). Okay... > there's one other change... if you pass 2 or more arguments, then > it will complain that it expected "at least 0 arguments", but try > it once with 0 arguments and you'll immediately understand. Could we perhaps deal with the latter issue by adding a *args to sorted's signature, and changing the condition on the 'if' to: if iteratorToSort is _privateObj or args: raise TypeError # etc etc ? Maybe w/"a single iterator argument" in the error message's text? {alternatively, if we don't care about keyword args, just having the *args in the signature and checking "if len(args) != 1: ..." might be OK} Alex
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4