On Jun 26, 2006, at 1:34 PM, MB Software Solutions wrote:
default parameter values - which was done much better in Clipper
<~g>
Which is done even better in Python!
Well don't just say that without an example!! Otherwise, you're
missing an opportunity to plug Dabo!!
It's not a Dabo thing; it's just the way Python works. You can pass
parameters by either position or by name, with named parameters
preferred due to less ambiguity. Any parameter can accept a default
value.
You can also have 'catch-all' arguments that handle variable numbers
of parameters. So, since you want an example, here goes:
def __init__(self, parent, properties=None, attProperties=None,
*args, **kwargs):
This initialization method accepts two positional parameters that
are required, since they have no default values: 'self' and 'parent'.
'self' is by convention the name used in all instance methods,
equivalent to VFP's 'This'. You can optionally pass the 'properties'
and/or 'attProperties' parameters; if you don't, they are initialized
with a value of None (VFP's .NULL.).
The 'catch-all' parameters are '*args' and '**kwargs', which is the
convention used for 'arguments' and 'key-word arguments'. If you pass
any additional unnamed parameters, their values will be in a list
(array) variable named 'args', and any additional named parameters
will be in a dictionary named 'kwargs', with the name as the key and
the value as the value. The asterisks are a shorthand for expanding
lists and dictionaries, respectively.
Why would anyone want to create methods with variable parameters?
Well, in Dabo you can initialize a control with as many properties as
you need. So I can create a button with:
btn = dabo.ui.dButton(mainPanel)
...which will create a standard button on the mainPanel object. Or, I
can write:
btn = dabo.ui.dButton(mainPanel, Caption="Click Me!", Left=42)
...which will do the same, except now the text caption and the left
edge will be set. If I later create a new property named 'Foo', and
add it to all my classes, I don't have to go back and change all the
initialization signatures for every class; I can just pass a Foo
parameter, and thanks to Dabo's property introspection, it will set
it without having to write additional code.
-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/profox
OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech
** All postings, unless explicitly stated otherwise, are the opinions of the
author, and do not constitute legal or medical advice. This statement is added
to the messages for those lawyers who are too stupid to see the obvious.