Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am trying to make sense of the following code snipet:

factor = misc.kkt_chol2(G, dims, A)
....
....
factor(W, P)

How the parameter in factor binding with the parameters inside kkt_chol2? Thank you in advance for the response.

share|improve this question

closed as unclear what you're asking by Martijn Pieters, MichaelT, Bart van Ingen Schenau, jwenting, amon May 8 at 14:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I am not sure I understand the sentence “How the parameter in factor binding with the parameters inside kkt_chol2?” – could you please reword it to make it clear? Anyway, the kkt_chol2 method returns another function, which is assigned to the factor variable. As the variable now contains a function, that function can be called as usual. –  amon May 7 at 22:33
    
I answered what I think is being asked, but the question could use some clarification. –  Steven Burnap May 7 at 22:52
    
Presumably misc.kkt_chol2 returns a function. Either one it selects, constructs or declares internally. That function will take the parameters. –  Jon Jay Obermark Aug 27 at 20:51

1 Answer 1

up vote 4 down vote accepted

The method that is returning factor is creating a closure. This binds the local scope to a function created internally. In other words, you do something like this:

def make_func(arg):
    def afunc():
        return arg;

    return afunc

foo = make_func(5)

print foo()  # prints '5'

You can find all the gory details here

share|improve this answer
    
It seems interesting!! So now my question is what is the relationship of G, dims, A in kkt_chol2 with W, P in factor? may be I am fool but I need to be clear. –  vessilli May 7 at 23:54
    
Dunno. Without seeing the code for kkt_chol2, it is impossible to say. –  Steven Burnap May 8 at 1:40
    
What comes from my common sense is that: kkt_chol2(G, dims, A) requires 3 parameter but factor(W,P) have only two!!! –  vessilli May 8 at 8:57
    
The parameters do not interact at all. The kkt function uses its parameters to select or construct the function it returns, and that function gets passed two parameters unrelated to those. The kkt function may store the original parameters internally, in which case the later call can still use them via closure, but that is no longer using the parameters, it is using references to them stored in local scope. So your answer is 'Not at all'. –  Jon Jay Obermark Aug 27 at 20:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.