"fumanchu" <[EMAIL PROTECTED]> writes:

> When you write normal Python functions, do you always use **kwds? The
> same reasons apply. Named arguments make your code cleaner, for sure,
> especially with default values. Compare:

The main thing is that normal functions / methods usually have fewer
parameters than a controller has.  When you have forms receiving something
like 80 or 90 input fields + checkboxes + multiple select fields + ... it
becomes *REALLY* problematic to handle these by hand.  

Using the dictionary cleans up the code a lot.

> def quadratic(a=1, b=2, c=3):
>     a, b, c = map(int, (a, b, c))
>     root = math.sqrt((b ** 2) - (4 * a * c))
>     return ((-b + root)/2a), ((-b - root)/2a))
>
> def quadratic(**kwds):
>     a = kwds.get('a', 1)
>     b = kwds.get('b', 2)
>     c = kwds.get('c', 3)
>     a, b, c = map(int, (a, b, c))
>     root = math.sqrt((b ** 2) - (4 * a * c))
>     return ((-b + root)/2a), ((-b - root)/2a))

You can always make this look like:

 def quadratic(**kwds):
     a, b, c = map(int, (kwds.get('a', 1), 
                         kwds.get('b', 2), 
                         kwds.get('c', 3)))
     root = math.sqrt((b ** 2) - (4 * a * c))
     return ((-b + root)/2a), ((-b - root)/2a))

(If you were using validators that "map" wouldn't be needed, making your code
 even clearer ;-))

> I usually reserve **kwds for times when I'm passing the kwds on to a
> common function for processing.

In my exclusive Python code I do the same.  For controllers I don't. 


-- 
Jorge Godoy      <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to