Re: another newbie question: why should you use "*args" ?




It's bad practice to use built-ins like 'list' as a regular variable
name.
ok, but it was just an example (in practice, I always use very long names ;-)
# calling method 1:
execute (S[0], S[4] )

# calling method 2:
execute ( ( S[0], S[4] ) )

Let's take a look at those side-by-side:
execute (S[0], S[4] )
execute ( ( S[0], S[4] ) )

Now, which one *looks* better?

# or *the extra flexibility)
mylist = ( S[0], S[4] )
execute ( mylist )

Also, take into consideration the opposite end of the pole; you have
your list of arguments (args), and your about to call a function that
was declared something like this:
def someFunction(arg1, arg2, arg3):
# etc.
Which is clearer?
someFunction(*args)
someFunction(args[0], args[1], args[2])

And if you've got a variable number of arguments, it becomes virtually
impossible to avoid using the *args syntax.

# So with this construct, I have all flavours:

def chunk_plot(*args):
if len(args)==1: my_example_var = args[0]
else: my_example_var = args
for i in range ( len ( my_example_var ) ):
... do something with my_example_var [i]

# calling the procedure
chunk_plot (S[1], S[4])
chunk_plot ( ( S[1], S[4] ) )
my_action_list = ( S[1], S[2] )
chunk_plot ( my_action_list )


And sorry, no need for kwargs for now ;-)

thanks guys,
Stef


Relevant Pages

  • Re: Very practical question
    ... fancy just routine stuff. ... def interClassCall: ... 2/ have the C instance passed as an argument to the calling method ... 4/ get the C instance from a module function ...
    (comp.lang.python)
  • Calling class method by name passed in variable
    ... Can someone suggest an efficient way of calling method whose name is ... def a: ... PHP code for this would be: ... I need a solution for Python. ...
    (comp.lang.python)
  • Re: [ANN] ParseTree 2.0.0 Released
    ... do you know a way to get at the parse ... tree of the calling method / block and at the node of the call? ... def calling_from_method ...
    (comp.lang.ruby)