Suppose I have a very simple function:
def myFunction(arg1, arg2):
return arg1, arg2
Now if I only want to access the second argument, can do I something like:
none, arg2 = myFunction(arg1, arg2)
?
Suppose I have a very simple function:
def myFunction(arg1, arg2):
return arg1, arg2
Now if I only want to access the second argument, can do I something like:
none, arg2 = myFunction(arg1, arg2)
?
Yes, yes you can do something like that. Exactly that, actually.
Assign the argument you want to ignore to a name you then simply not use anywhere else. The _ name is often used:
_, arg2 = myFunction(arg1, arg2)
The _ name is just a convention, but most Python developers understand it to mean 'this variable will be ignored by the rest of the code, it is just there to fulfill a tuple assignment'.
Alternatively, index or slice the output:
arg2 = myFunction(arg1, arg2)[-1]
or for a function with more than two return values:
arg2, arg3 = returnsMoreThanTwo(arg1, arg2)[-2:]
but if you wanted to ignore an argument in the middle somewhere, the _ assignment is best. You can use it multiple times without penalty:
arg1, _, _, arg4 = returnsFourArguments(arg1, arg2)
first_and_something = operator.itemgetter(0, 60) then first, something = first_and_something(returnsWhateverArguments(arg1, arg2)) or whatever...itemgetter() approach is fair game. :-P_, arg2 = myFunction(arg1, arg2)
_ can be used. Not that I voted on your post..python questions. Never seen something like this in postgresql or sql-server. Don't know why. OTOH, it works for upvotes too - once I had answer about how to join list into string (like ' '.join(a)) with 11 upvotes!!! To get 11 upvotes in sql-server section I have to create something really cool :)