1

I am beginner in python and I am trying to write something like this in objective-c. In Objective-C, each method can be stored in block and those can be called again later. I have tried to find online but I don't know keyword in python. Can I write like that in python or is there any similar flow ? How shall I do?

void (^successBlock)() = ^{
    NSLog(@"Test");
};

NSMutableArray *muArr = [NSMutableArray array];
[muArr addObject:successBlock];

for (void (^successBlock)() in muArr) {
    successBlock();
}
Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120

3 Answers3

3

Functions are first-class members of Python. There's no extra syntax required beyond function definition. Check out this example using just standard library functions and a lambda function (which is roughly analogous to a block).

functions = [round, int, str, lambda x: x + 2]

for fn in functions:
    print(fn(3.1415))
# 3
# 3
# 3.1415
# 5.141500000000001
Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56
1

Functions in Python are first class citizens and so can be stored in variables.

See Assigning a function to a variable

def successBlock():
    NSLog("Test")

muArr = [successBlock]

for sb in muArr: 
    sb()
Community
  • 1
  • 1
user783836
  • 3,099
  • 2
  • 29
  • 34
-1

Try this code and it should work

void (^successblock)() = ^^{
    NSLog(@"Test");
};

NSMutableArray *mUArr = [NsMutableArray array];
[muArr addObject:successBlock];

for (void (^successBlock)() in muArr) {
    successBlock()
}
T Joe
  • 1