简体   繁体   中英

Python dict call function as value

I'm creating a dict in python using returned json.

one of the values I want to be a shortuuid, so I put a function as the value. I want that function called and the value replaced with what that function returns.

Is this correct ?

        tvMSG = {'fromEMAIL': uEmail, 'toCHANNELID': channelID, 'timeSENT': uTime}
             for msgkey, subdict, instakey in (('profilePIC', 'user', 'profile_picture'),
                         ('userNAME', 'user', 'username'),
                         ('msgBODY', 'caption', 'text'),
                         ('mainCONTENT','images', 'standard_resolution',)
                         ('tvshowID', shortuuid.uuid())):



   this is the key/value in question:
            ('tvshowID', shortuuid.uuid())):

This is the error I get:

        TypeError: 'tuple' object is not callable

If not how do I make it work?

The error comes from a missing comma. The line:

('mainCONTENT','images', 'standard_resolution',)

should actually be:

('mainCONTENT','images', 'standard_resolution'),

That's why you were getting the error 'tuple' object is not callable , you were calling ('any','tuple')('arguments') .

Thanks for the hints @ThierryJ.

  tvMSG = {'fromEMAIL': uEmail, 'toCHANNELID': channelID, 'timeSENT': uTime, 'tvshowID: shortuuid.uuid()}
         for msgkey, subdict, instakey in (('profilePIC', 'user', 'profile_picture'),
                     ('userNAME', 'user', 'username'),
                     ('msgBODY', 'caption', 'text'),
                     ('mainCONTENT','images', 'standard_resolution')):

There was no need to do any iteration. I just took that call out of the loop and put it in the variable above the loop.

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