[Python-3000] Iterators for dict keys, values, and items == annoying :)

Benji York benji at benjiyork.com
Sat Apr 1 17:46:47 CEST 2006


Greg Ewing wrote:
> Benji York wrote:
>>If I, as the user of the interface, have something I want to 
>>pass in that doesn't match I it to the appropriate interface the burden 
>>is on me to create something that matches expectations.  People do that 
>>all the time today without an interface/adaption framework, they just 
>>write code that takes one thing and builds another.
> 
> Yes, and that seems like a fine way of doing it. You
> know what you've got and where you want to get to, so
> there's no need to look anything up in a registry.

There are two benefits.  The first comes when you need to do it more 
than once, so you don't end up designing an API to go from the one thing 
to another.  Instead you have an adaptation framework to work within. 
The second is that you can generalize and not have to know what, 
exactly, you're converting from, if it is, or has an adapter to, the 
correct thing, you're good.  Much like duck typing.


>>Instead of building an API for looking up security descriptions from a 
>>user name that I have to pull out of the user object, I could instead 
>>register and adapter from IUser to ISecurityInfo,
> 
> I don't see how this is better than just calling
> a get_security_info_from_user() function

Less code.  Instead of designing a special purpose API to retrieve one 
form of information given another, you adapt.

>>  Looping over the form fields and adapting each to IWidget and getting 
>>back a TextField for a string, CheckBox for a boolean, etc.
> 
> Here I don't see how it's better than passing in
> a dict mapping field classes to widget classes.
> Worse, in fact, since it would seem to restrict
> you to a single global mapping for all forms.

Again, simpler API.  You're freed from passing around (or building) lots 
of mini-registries of things and defining APIs for each.

>>once you have the simple tools of adaptation in mind
>> ... you start to recognize places where they help
>>you solve problems in better ways.

> Or they lead you into an everything-is-a-nail
> mode of thinking, which is what the above seem
> to be to me, to some extent.

Adaptation, like anything else, can be overused.  There are situations, 
like the above, where adaptation can improve the code.  Seems pretty 
Pythonic to me, just as dicts and lists can be used (well) in lots of 
situations, likewise for adaptation.
--
Benji York


More information about the Python-3000 mailing list