[Python-Dev] Iterable String Redux (aka String ABC) (original) (raw)
Michael Foord fuzzyman at voidspace.org.uk
Fri May 30 18:17:02 CEST 2008
- Previous message: [Python-Dev] Iterable String Redux (aka String ABC)
- Next message: [Python-Dev] Iterable String Redux (aka String ABC)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Armin Ronacher wrote:
Greg Ewing <greg.ewing canterbury.ac.nz> writes:
Well, I'm skeptical about the whole ABC thing in the first place -- it all seems very unpythonic to me. I think it's very pythonic and the very best solution to interfaces and duck typing. Not only does it extend duck-typing in a very, very cool way but also does it provide a very cool way to get custom sets or lists going with few extra work. Subclassing builtins was always very painful in the past and many used the User* objects which however often broke because some code did something like isinstance(x, (tuple, list)). Of course one could argue that instance checking is the root of all evil but there are situations where you have to do instance checking. And ABCs are the perfect solution for that as they combine duck-typing and instance checking. In my oppinion ABCs are the best feature of 2.6 and 3.0. But another way of thinking about it is that we already have an ABC of sorts for strings, and it's called basestring. It might be better to enhance that with whatever's considered missing than introducing another one. basestring is not subclassable for example. Also it requires subclassing which ABCs do not.
I would be strongly +1 on a string ABC. Currently (to my knowledge) there is no way of using duck typing for built-in APIs that expect a string. How do I pass in an object to 'open' for example that isn't actually a string or subclass?
class X(object): ... def unicode(self): ... return 'fish' ... str = repr = unicode ... x = X() open(x) Traceback (most recent call last): File "", line 1, in TypeError: coercing to Unicode: need string or buffer, X found unicode(x) u'fish'
Michael Foord
Regards, Armin
Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.uk
-- http://www.ironpythoninaction.com/ http://www.theotherdelia.co.uk/ http://www.voidspace.org.uk/ http://www.ironpython.info/ http://www.resolverhacks.net/
- Previous message: [Python-Dev] Iterable String Redux (aka String ABC)
- Next message: [Python-Dev] Iterable String Redux (aka String ABC)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]