On Thursday 30 October 2003 06:49 am, Raymond Hettinger wrote: ... > Universal methods give the method a way to handle the two > cases separately. This provides both the capability to make > an instance from scratch or to copy it off an existing instance. > > Your example was especially compelling: > > a = [3,2,1] > print a.sorted() > print list.sorted(a) Actually, yes, it IS compelling indeed. Funny -- I was originally just brainstorming / musing out loud, never thought about this as a "real thing". But now that it's matured a bit, I do feel sure -- from past experience with what puzzles Python newbies depending on their previous programming knowledge or lack thereof -- that if we had this it would *seriously* reduce the number of puzzlements we have to solve on c.l.py or help at python.org. Which IS strange, in a way, because I do not know of any existing language that has exactly this kind of construct -- a factory callable that you can call on either a type or an instance with good effect. Yet despite it not being previously familiar it DOES "feel natural". Of course, the 3 lines above would also work if sorted was an ordinary instancemethod, but that's just because a is a list instance; if we had some other sequence, say a generator expression, print list.sorted(x*x for x in a) would be just as sweet, and _that_ is the compelling detail IMHO. Trying to think of precedents: Numeric and gmpy have quite a few things like that, except they're (by necessity of the age of gmpy and Numeric) ordinary module functions AND instance methods. E.g.: >>> gmpy.sqrt(33) mpz(5) >>> gmpy.mpz(33).sqrt() mpz(5) >>> gmpy.fsqrt(33) mpf('5.74456264653802865985e0') >>> gmpy.mpf(33).sqrt() mpf('5.74456264653802865985e0') as a module function, sqrt is the truncating integer square root, which is also a method of mpz instances (mpz being the integer type in gmpy). mpf (the float type in gmpy) has a sqrt method too, which is nontruncating -- that is also a module function, but, as such, it needs to be called fsqrt (sigh). I sure _would_ like to expose the functionality as mpf.sqrt(x) and mpz.sqrt(x) [would of course be more readable with other typenames than those 'mpf' and 'mpz', but that's another issue, strictly a design mistake of mine]. 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