sirvon
sirvon

Reputation: 2625

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?

Upvotes: 2

Views: 254

Answers (2)

Thierry J
Thierry J

Reputation: 2189

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').

Upvotes: 4

sirvon
sirvon

Reputation: 2625

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.

Upvotes: 0

Related Questions