It's a good idea to change the formatting, before trying to understand what it does:
def label(self, index, *args):
"""
Label each axes one at a time
args are of the form <label 1>,...,<label n>
APIPARAM: chxl
"""
self.data['labels'].append(
str( '%s:|%s' % \
( index, '|'.join( map( str,args ) ) )
).replace( 'None', '' )
)
return self.parent
So:
it appends something to self.data[ 'labels' ]
list. We know this because append()
is a method of list object.
This something is a string such that:
- string is of the form
xxx:|yyy
xxx
is replaced with the value of argument index
yyy
is replaced with all the other arguments converted to strings (map(str,args)
) and joined with |
character (join(...)
) so resulting in something like 'a|b|None|c
'
- every occurence of
None
in the string above is replaced with an empty string and this is appended to the list
EDIT:
As @abarnert pointed out it might be good to explain what does *args
mean and why later on it's used as args
, so here it goes.
*args
(which is an asterisk + an arbitrary name) means "any number of anonymous arguments available further in args
list". One can also use **kwargs
- note two asterisk which is used for accepting keyworded arguments, i.e. ones passed to the function in the form of foo = bar
where foo
is the name of the argument and bar
is its value rather than just bar
.
As said above args
and kwargs
are arbitrary, one could just as well use *potatoes
or **potatoes
but using args
and kwargs
is a convention in Python (sometimes people also use **kw
instead of **kwargs
, but the meaning is the same - any number of anonymous and any number of keyworded arguments respectively).
Both are used if the number of arguments which the function/method should accept is not known beforehand - consider for a example a function which processes names of the party guests, one may not know how many there may be, so defining a following function makes sense:
def add_party_quests( *quests ):
for guest in quests:
do_some_processing( guest )
Then both calls below are valid:
add_party_guests( 'John' )
add_party_guests( 'Beth', 'Tim', 'Fred' )
This is also explained in this SO post: https://stackoverflow.com/a/287101/680238