git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Python refactoring question and create dynamic attributes


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