Python: pass method as argument in function
I've seen a lot of posts but none really addressing my question. With Python, I am trying to pass a method as an argument in a function which requires two arguments:
# this is a method within myObject
def getAccount(self):
account 开发者_开发知识库= (self.__username, self.__password)
return account
# this is a function from a self-made, imported myModule
def logIn(username,password):
# log into account
return someData
# run the function from within the myObject instance
myData = myModule.logIn(myObject.getAccount())
But then Python's not happy: it wants two arguments for the logIn() function. Fair enough. If thought the problem was that the getAccount() method returned a tuple, which is one object. I tried then:
def getAccount(self):
return self.__username, self.__password
But that either did not make a difference.
How then can i pass the data from getAccount() to logIn()? Surely if i don't understand that, i am missing something fundamental in the logic of programming :)
Thanks for helping. Benjamin
You want to use python argument unpacking:
myData = myModule.logIn( * myObject.getAccount() )
The *
before an argument to a function signals that the following tuple should be split into its constituents and passed as positional arguments to the function.
Of course, you could do this by hand, or write a wrapper that takes a tuple as suggested by others, but unpacking is more efficient and pythonic for this case.
This
myData = myModule.logIn( * myObject.getAccount() )
Your method asks for two parameters while if you want to hide the login execution in a method you can easily pass one parameter, execute it and retrieve the data:
# this is a method within myObject
def getAccount(self):
account = (self.__username, self.__password)
return account
# this is a function from a self-made, imported myModule
def logIn(account):
user , passwd = account()
# log into account
return someData
# run the function from within the myObject instance
myData = myModule.logIn(myObject.getAccount)
Note that the method is passed without the parenthesis, then you execute it within the login retrieving the data.
Your title is a bit confusing, since you actually can pass a method. In Python functions are first class, which means you can pass them around as any value.
Your text however shows that you want to do something else.
Python really returns multiple values as one value, a tuple of the values.
return 1, 2
is really the same as
return (1, 2)
However, if you need to unpack these values, as in your case, there are several ways to achieve this.
You can unpack them into variables:
usn, psw = myObject.getAccount()
Or you can "expand" them right into the function call:
myModule.logIn(*myObject.getAccount())
This requires that the amount of arguments is the same as the dimension of the tuple (2 argument function requires a tuple (x, y)
)
If you instead want to pass the method you can do that, but you need to be careful not to call it:
def logIn(self, a):
usn, psw = a()
# do stuff
logIn(myObject.getAccount)
精彩评论