Łukasz Langa added the comment:

There are several reasons why `get*()` methods are redefined on the section 
proxy. First of all, explicit is better than implicit. Secondly, the order of 
arguments is different: `parser.get()` has the fallback argument as the last 
(and keyword-only), whereas `parser['section'].get()` behaves like a mapping. 
You can do `parser['section'].get('key', 'some-fallback-value')` and 
`parser['section'].get('no-such-key')` returns None instead of raising 
`NoOptionError`.

This makes it difficult to automagically support parser `get*()` methods on the 
section proxy. This is why I decided to adapt a different mechanism: register a 
converter and a getter is automatically available:

  >>> cp = ConfigParser()
  >>> cp.converters['list'] = lambda value: value.strip().split()
  >>> cp.getlist('section', 'l')
  ['a', 'b', 'c']
  >>> cp['section'].getlist('l')
  ['a', 'b', 'c']
  >>> cp.getdict('section', 'd')
  Traceback (most recent call last):
  ...
  AttributeError: 'ConfigParser' object has no attribute 'getdict'
  >>> cp['section'].getdict('d')
  Traceback (most recent call last):
  ...
  AttributeError: 'ConfigParser' object has no attribute 'getdict'

This ensures that you can easily add new converters in subclasses or single 
instances and that the parser-level API and section-level API work like they 
should. This also makes implementing custom getters easier since there's no 
logic involved besides the conversion. And if you happen to need custom logic 
anyway, you can register a converter that is a callable class.

----------
keywords: +patch
stage:  -> patch review
type: behavior -> enhancement
Added file: http://bugs.python.org/file30704/issue18159-3.diff

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18159>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to