Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13054
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Applying a function recursively |
| References | <52c256ae-81b0-4a7c-9047-42e1ae8b6eda@n19g2000prh.googlegroups.com> |
| Date | 2011-09-10 18:13 +1000 |
| Message-ID | <87ipp0n7c1.fsf@benfinney.id.au> (permalink) |
| Organization | Unlimited download news at news.astraweb.com |
hetchkay <hetchkay@gmail.com> writes: > Hi, > I want to apply a "convert" function on an object as follows: > If the object is of MyType type, invoke the passed in function. > If the object is a dictionary, apply on the keys and values of the > dictionary recursively. > If the object is a set, list or tuple, apply on each element > recursively. > Else, leave the object as is. That smells like a bad design. Why are you using the same function for al of those different behaviours? That's not merely rhetorical; the design isn't absolutely wrong. But it's wrong often enough that you need to have a compelling reason to make such a complex behaviour in a single function. I suspect, if you can be explicit about the goal you're aiming for with this code, a better design can be found that doesn't require all those polymorphism-breaking type checks. -- \ “An expert is a man who has made all the mistakes which can be | `\ made in a very narrow field.” —Niels Bohr | _o__) | Ben Finney
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Applying a function recursively hetchkay <hetchkay@gmail.com> - 2011-09-10 00:19 -0700
Re: Applying a function recursively Chris Rebert <clp2@rebertia.com> - 2011-09-10 00:40 -0700
Re: Applying a function recursively Ben Finney <ben+python@benfinney.id.au> - 2011-09-10 18:13 +1000
Re: Applying a function recursively hetchkay <hetchkay@gmail.com> - 2011-09-10 06:28 -0700
Re: Applying a function recursively Roy Smith <roy@panix.com> - 2011-09-10 10:12 -0400
csiph-web