How can I iterate over an object and assign all it properties to a list
From
a = []
class A(object):
def __init__(self):
self.myinstatt1 = 'one'
self.myinstatt2 = 'two'
to
a =['one','two']
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Don’t create a full fledged class if you just want to store a bunch of attributes and return a list so that your API can consume it. Use a
namedtupleinstead. Here is an example.If your API just expects a sequence (not specifically a
list), you can passpdirectly. If it needs a list specifically, it is trivial to convert thePointobject to a list.You can even subclass the newly created
Pointclass and add more methods (documentation has details). Ifnamedtupledoesn’t meet your needs, consider sub-classingabc.SequenceAbstract Base Class or using it as a mixin.