Humble Hero
Humble Hero

Reputation: 145

python method as variable

Python (3.7)

I am trying to write a simple function that returns metadata from a Salesforce object (table), where table name is an argument. in the example below Booking__c is an object(table) name and it returns collections.OrderedDict as expected.

from simple_salesforce import Salesforce
sf=Salesforce(username,password,organizationId)

table_info=sf.Booking__c.describe()

However, I need to write a function so I can reuse it for multiple tables. I tried exec() but it returns NoneType

def all_columns(table):
    c = exec("sf.table.describe()")
    return c

I know that exec() is not recommended to use, so would appreciate a piece of advice if there is a proper way to achieve this, or at least help with exec.

Upvotes: 1

Views: 49

Answers (1)

Carcigenicate
Carcigenicate

Reputation: 45750

I think you'd need eval here instead if you need a return value, as I believe exec is purely for executing side effects.

There's no need for either though. You can access an attribute of an object by name using getattr:

c = getattr(sf, table).describe()

Upvotes: 2

Related Questions