Python refactoring question and create dynamic attributes
Arup Rakshit
ar at zeit.io
Sun Jun 23 03:56:51 EDT 2019
In the below code:
@classmethod
def find(self, id):
if isinstance(id, list):
rows = self.__table__().get_all(*id).run(self.__db__().conn)
result = []
for row in rows:
acategory = Category()
acategory.__dict__.update(row)
result.append(acategory)
return result
else:
adict = self.__table__().get(id).run(self.__db__().conn)
acategory = Category()
acategory.__dict__.update(adict)
return acategory
I have 2 questions:
1. Is there any better way to create attributes in an object without using __dict__().update() or this is a correct approach?
2. Can we get the same result what for row in rows: block is producing without killing the readability ?
To see the context, here is my source code https://gitlab.com/aruprakshit/flask_awesome_recipes/blob/master/app/models/category.py
Thanks,
Arup Rakshit
ar at zeit.io
More information about the Python-list
mailing list