简体   繁体   中英

python inner function with argument

Can someone help me with this python code. I am a beginner with Python, good with java though.

def train_nn(iterations, batch_size, use_tf_mnist=False):
    def perPartition(it):
        if not use_tf_mnist:
            train_data = RowData(it)
            test_data = train_data
        else:
            from tensorflow.examples.tutorials.mnist import input_data
            mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
            train_data = mnist.train
            test_data = mnist.train

        return create_nn(train_data, test_data, iterations, batch_size)

    return perPartition

the caller only calls train_nn(ITERATIONS, BATCH_SIZE, USE_TF_MNIST) . So where does the inner function get its it argument from?

train_nn returns the perPartition function as a result. It gets its argument when the caller later calls the function:

trained = train_nn(100, 50, False)
nn = trained(some_it)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM