[Python-Dev] New syntax for 'dynamic' attribute access (original) (raw)

Guido van Rossum guido at python.org
Tue Feb 13 01:32:07 CET 2007


Oh, now I am definitely in favor of .[]! I read it in gmail in FireFox which uses a small variable-pitch font whose dot is a single pixel. The .() example was hard to find; the .[] jumped out immediately. (When do you ever see self[anything]?)

On 2/12/07, Brett Cannon <brett at python.org> wrote:

On 2/12/07, Guido van Rossum <guido at python.org> wrote: > FWIW, I'm strongly -1 on the "->" notation. As a C programmer it's got > too many neurons committed to it. > > I recommend that you do some experiments with the readability of the > .[...] notation, e.g. write a program that randomly generates x.[foo] > and x[foo], and see how fast you can spot the difference. I bet that > you won't have any trouble. >

OK, so real-world examples. First, foo.(name), from urllib.py:: name = 'open' + urltype self.type = urltype name = name.replace('-', '') if not hasattr(self, name): if proxy: return self.openunknownproxy(proxy, fullurl, data) else: return self.openunknown(fullurl, data) try: if data is None: return self.(name)(url) else: return self.(name)(url, data) except socket.error, msg: raise IOError, ('socket error', msg), sys.excinfo()[2] and also:: name = 'httperror%d' % errcode if hasattr(self, name): method = self.(name) if data is None: result = method(url, fp, errcode, errmsg, headers) else: result = method(url, fp, errcode, errmsg, headers, data) if result: return result return self.httperrordefault(url, fp, errcode, errmsg, headers)

And here is urllib2.py for .[] (used different files so you wouldn't just remember where the change was):: if attr[:12] == 'Request_r': name = attr[12:] if hasattr(Request, 'get' + name): self.'get' + name return self.[attr] raise AttributeError, attr and:: handlers = chain.get(kind, ()) for handler in handlers: func = handler.[methname] result = func(*args) if result is not None: return result

Neither version jumps out at me strongly, although between the two the .[] version shows up the best. But that might also be because of the lower noise when used in a call. -Brett

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list