0

I am generating list of class variables as below and i need print the value of those variable.

class MYClass(object):

    a = '1'
    b = '20'
    c = 'hello'

    def as_list(self):
        return [attr for attr in dir(MYClass()) if not callable(attr) and not attr.startswith("__") and not attr == 'as_list']


c = MYClass()
print c.as_list()

The above code would result in ['a', 'b', 'c'] but i need the value as ['1', '20', 'hello']

2
  • 2
    Wouldn't dir(self) instead of dir(MYClass()) be more appropriate? Commented Mar 2, 2017 at 11:51
  • Actually that is static method so self can't be used , for pasting here i made some minor changes to code sample Commented Mar 3, 2017 at 6:14

1 Answer 1

2

I'm not sure why you are doing this, but instead of returning attr from your list comprehension you should return getattr(self, attr).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.