Python: pass method as argument in function?

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.

Thanks for detailing the use of * :) – Benjamin May 4 at 10:44 For the record, it works with any iterable, it will just be exhausted completely before the function is called. – delnan May 4 at 13:20.

This myData = myModule. LogIn( * myObject.getAccount() ).

It is not C, in python you just use the method name – correndo May 4 at 10:06 @correndo: That's true if you want to pass a callback. If you don't, He is correct. – the_drow May 4 at 10:07 1 @correndo: This is unpacking.

The return value of myObject.getAccount() is unpacked so that it becomes two arguments. Why do you think he wrote C? – Felix Kling May 4 at 10:10 1 I don't understand this, could you explain the use of * please?

– Benjamin May 4 at 10:29 1 @Benjamin: * takes the tuple and unpacks it into the myModule.logIn() method. So while myObject.getAccount() returns username, password the * operator transforms them into the parameters of myModule.logIn(). – the_drow May 4 at 10:48.

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.

I find this answer interesting, I didn't even realise I could do that. However I find it less readable, because it's not clear when reading the logIn() function what the account() really does. – Benjamin May 4 at 10:37.

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).

Thanks for providing a detailed answer. I didn't know most of what you explained. As I commented above, I am not sure as to how the use of * functions, although it seems to be the most elegant way to achieve what I want to do.

Also I am not really sure I understand what you said about the title of this question. – Benjamin May 4 at 10:39 @Benjamin: Never mind that about the title. It was just a very obtuse way to say I misunderstood you :) You could say the * operator maps each value of an iterable to a positional argument in a function call (it works with lists as well.) There is also the ** operator, which unpacks a dict into keyword arguments.

– Skurmedel May 4 at 13:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions