简体   繁体   中英

Cannot understand this in-line piece of code in the facebook-sdk for python

I'm guessing that this is a short hand form of writing a block of code. Could somebody expand this piece of code to look like a regular function?

[some_action(post=post) for post in posts['data']]

Also what does post=post do in the expression?

It's a list comprehension .

Long code:

result= []
for post in posts['data']:
    value= some_action(post=post)
    result.append(value)
def func(some_action, posts):
  res = []
  for post in posts['data']:
    res.append(some_action(post=post))
  return res
[some_action(post=post) for post in posts['data']]

This is a short hand representation of a long function in which some_action is a function which accepts an argument, the first post is the argument and second post sets up the internal parameter for the function some_action, which actually is an event if you look at it closely.

suppose the some_action is a simple print function this would be written in this way in long hand

for post in posts['data']:
 print_post(post=post)

This will assign the each value of post in posts to an internal parameter named post.

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