Python: pass a method as an argument in a function

I saw a lot of posts, but no one addressed my question. With Python, I am trying to pass a method as an argument to a function that 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 is not happy: it needs two arguments for the logIn () function. Fair. If you think about it, the problem is that the getAccount () method returned a tuple, which is a single object. I tried:

 def getAccount(self): return self.__username, self.__password 

But that didn't matter either.

How can I pass data from getAccount () to logIn ()? Of course, if I don’t understand this, I’m missing something fundamental in the programming logic :)

Thanks for the help. Benjamin

+6
source share
4 answers

You want to use python argument unpacking :

 myData = myModule.logIn( * myObject.getAccount() ) 

* before the function argument signals that the next tuple should be divided into its components and passed as positional arguments to the function.

Of course, you could do it manually or write a shell that takes a tuple as suggested by others, but unpacking is more efficient and pythonic for this case.

+8
source

it

 myData = myModule.logIn( * myObject.getAccount() ) 
+6
source

Your method asks for two parameters, and if you want to hide the input to the method, you can easily pass one parameter, execute it and get 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 a bracket, then you execute it at the time of logging in, receiving the data.

+3
source

Your name is a bit confusing since you can really pass a method. Python functions are first class, which means you can pass them as any value.

However, your text indicates that you want to do something else.

Python really returns multiple values ​​as a single value, a tuple of values.

 return 1, 2 

really the same thing

 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 directly in a function call:

 myModule.logIn(*myObject.getAccount()) 

This requires the number of arguments to be the same as the size of the tuple (function 2 of the argument requires a tuple (x, y) )

If you want to pass a method, you can do it, but you need to be careful not to name it:

 def logIn(self, a): usn, psw = a() # do stuff logIn(myObject.getAccount) 
+3
source

Source: https://habr.com/ru/post/887263/


All Articles