Ser.T
Ser.T

Reputation: 51

How do i find out what attributes in a python class are callable?

sample_list = []

for attribute in dir(sample_list):
    if callable(attribute):
        print(attribute)

I'm writing this code to find out what attributes of sample_list are callable. I know the list class has callable attributes like append and pop but none of them seem to show up. why's this?

Upvotes: 1

Views: 350

Answers (1)

khelwood
khelwood

Reputation: 59240

attribute is a string. A string isn't callable. You're missing getattr(sample_list, attribute).

sample_list = []

for attribute in dir(sample_list):
    value = getattr(sample_list, attribute)
    if callable(value):
        print(attribute)

Upvotes: 2

Related Questions