0

I have just seen the following code and would like to know what the function within the __init__ function means

def __init__(self):
    def program(percept):
        return raw_input('Percept=%s; action? ' % percept)
    self.program = program
    self.alive = True
Ethan Bierlein
  • 3,353
  • 4
  • 28
  • 42
  • 1
    @CoryKramer This is not a duplicate. The linked question is about what `__init__` means in a class. This question is about defining a function within a function (which happens to be `__init__`). I believe this should be reopened, unless a more accurate duplicate question can be found. – Platinum Azure Oct 06 '16 at 17:07
  • @PlatinumAzure If that is indeed what the OP is confused about, they should specify. From what I understood their question to mean, they wanted to know what the `__init__` method was used for in general. I will reopen if the OP clarifies their question and it is indeed not a duplicate. – Cory Kramer Oct 06 '16 at 17:10
  • 1
    @CoryKramer The question has some grammatical issues, but the intent is clear: "I have just seen this code and would like to know what **the function with in the `__init__` function** actually means" (emphasis mine). – Platinum Azure Oct 06 '16 at 17:12
  • 1
    If they are confused about what nesting a function within another function does, then [this duplicate link](http://stackoverflow.com/questions/4831680/function-inside-function) should answer that – Cory Kramer Oct 06 '16 at 17:12
  • @CoryKramer Nice find. If you can switch the duplicate link in the question to point to the one you have just found, I will withdraw my reopen vote. Thanks! (Edit: Question is open again, I have voted to close. Thanks!) – Platinum Azure Oct 06 '16 at 17:13
  • @CoryKramer Thanks for the feedback, I know what the constructor does, i just want to know what defining a function with in the constructor does – Solomon Bbaale Oct 06 '16 at 17:16
  • It's called a closure, as pointed out by the duplicate. `self.program = program` is using that defined function as an object – OneCricketeer Oct 06 '16 at 17:17
  • @CoryKramer From what I understand the constructor is used to initialize the object and it does not therefore return other functions. In this case when is the program function ever used. The example you referred to was in a callable function which returned another function that I could then execute – Solomon Bbaale Oct 06 '16 at 17:25
  • @cricket_007 thanks, I had missed that part of the code, long day I suppose.Sorry guys my bad I just needed to look a little closer to the code, thanks again – Solomon Bbaale Oct 06 '16 at 17:30

1 Answers1

0

This code

class Foo:
    def __init__(self):
        def program(percept): 
            return raw_input('Percept=%s; action? ' % percept)
        self.program = program

AFAIK, is actually the exact same as this since self.program = program.

class Foo:
    def __init__(self):
        pass

    def program(self, percept): 
        return raw_input('Percept=%s; action? ' % percept)

The only reason I can see to nest a function there was if you just used the function to do some initializations within the constructor, but didn't want to expose it on the class.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245