Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Optional and named parameters in Python

When you're calling a named block of code (either a function or a method in Python terms), you'll pass in a number of parameters. Some of those will be mandatory - needed every time. Some will be optional, but frequently used, and others will be wanted on very rare occasions indeed and selected from a potentially huge number of possibilities.

Calls to a Python function (and the matching declaration) normally start with the mandatory parameters, followed by the optional but most-often-wanted ones, and finally the less common ones.

• There are some straightforward functions defined and called in this example (opens in new window, as do all following source links)

• There are some fucntion calls making use of optional parameters, and calling functions in a separately loaded file here and the function file is here.

• There's an example of "call by name" where the caller tells the function which parameter is which, and also an example of a call that puts a whole load of values into a single list (like sponging up remaining paramaters) here

And finally - a new example from yesterday which I'll explain in a bit more detail - main source code here and the module in calls here. What does it show?

1. You can bring in another file of functions into your current namespace using the from statement. You can bring those function in to a namespace that's the same as the file name from which they are loaded by using the import statement. And (new demo) you can use import as to bring them in from a file but to a different namespace. Example:
  import taxcalcs as tc

Why would you want to do this? Because you might have some long file names and paths that you don't want to have to keep stating, or you might have two alternative modules and wish to change the code simply by changing a single import statement.

2. If you add a parameter with two asterixes (**) on the end of a function definition, that parameter is taken as being the name of a dictionary into which all otherwise unidentified parameters are stored.

Why would you want to do this? to give a huge flexibility to the call - being able to pass in anything you wished without the need for each item passed in to set up its own variable. It's much more secure as a function writer to palce all your user's named additions into a single dictionary than to allow them to spill all over your namespace.

Example call:
net = tc.getnet(amount,country="Slovenia",taxrate=20,
  year=2008,type="VAT")


Example function:
def getnet(gross, taxrate=17.5, **others):
  net = gross / (1.0 + taxrate*0.01)
  for more in others.keys():
    print "GETNET:",more," - ",others[more]
  return neto


Part of the sample output:
How much did you spend? 120.00
GETNET: country - Slovenia
GETNET: type - VAT
GETNET: year - 2008
That was 100.0 for the merchant (Slovenia)

(written 2008-11-05 03:44:47)

 
Associated topics are indexed under
Y105 - Python - Functions, Modules and Packages

Back to
What to do with a huge crop of apples
Previous and next
or
Horse's mouth home
Forward to
Barack Obama wins US Predidency
Some other Articles
What are exceptions - Python based answer
Is it worth it?
List Comprehensions in Python
Barack Obama wins US Predidency
Optional and named parameters in Python
What to do with a huge crop of apples
Anonymous functions (lambdas) and map in Python
Liverpool - a friendly city
Domain Renewal Group
What a difference a day made
1975 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho