November 7th, 2004, 09:16 AM
-
Smalltalk style named parameters
Named parameters
I what to know how to create named parameters, like how they do in smalltalk. So ordering isn't required. Example
person =method(age=”65”,name=”Gordon Brown”,sex=”male”) <-is the same as
person =method(sex=”male”,name=”Gordon Brown”,age=”65”) <- this
November 7th, 2004, 11:21 AM
-
You can do exactly that with any Python function/method call - just call the function with its parameter list like you did above.
November 8th, 2004, 06:42 AM
-
This example shows several ways of passing arguments to a function/methods; ordered and unordered:
Code:
>>> def foo(param1, param2, param3):
... print param1, param2, param3
...
>>> foo('one', 'two', 'three')
one two three
>>> foo(param1 = 'one', param3 = 'three', param2 = 'two')
one two three
>>> params = ('one', 'two', 'three')
>>> params
('one', 'two', 'three')
>>> foo(*params)
one two three
>>> params = {'param1': 'one', 'param3': 'three', 'param2': 'two'}
>>> params
{'param3': 'three', 'param2': 'two', 'param1': 'one'}
>>> foo(**params)
one two three
>>>
Mark.