monkeyman79 monkeyman79 - 1 year ago 118
Python Question

Python: Is it possible to skip values in tuple assignment?

I have a function which splits string in two parts at first encountered colon (skipping parts enclosed in brackets). This function returns tuple of three elements: index where the colon was encountered, part before colon and part after colon:

def split_on_colon(str):
colon_ptr = find_separator(str, 0, ':')
if colon_ptr == -1:
return (colon_ptr, str, None)
return (colon_ptr, str[:colon_ptr], str[colon_ptr+1:])

I call it this way:

def substitute_expression(expression):
# Split function and arguments
colon_ptr, func, args = split_on_colon(expression)

But sometimes it don't care about

Is there any simple construction in Python that would allow throwing away part of tuple in assignment, so that it wouldn't waste memory and variables namespace?

Something like this:

Ignore, func, args = split

Answer Source

You can use the _, which is used to store unwanted values. Your statement will look like this:

_, func, args = split
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download