Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, beginner's question! Repository or Object persisting itself?

Tags:

python

I am a seasoned .Net developer who's trying to write some Python code. On one of the projects I am contributing to, we have a services layer which is a set of classes which abstract away functionality and a django web app which consumes these in process services (which are just classes).

I had created a repository layer and ensured that all interaction with the database happens through the services layer through this repository. We have a document oriented database and thus we do not have the usual object-relational muck. During a recent code review, one developer who is supposedly seasoned with python shunned at this and commented that this was not the python way of doing things. He remarked that python developers are used to having a save and delete method on the object instance itself (and do not use the repository pattern as much) and this would confuse python devs looking to contribute to our OSS project. Python devs, your views? Would you be confused?

Edit: This is not django code, but will be code called by the django app (It an in process service layer)

like image 726
SharePoint Newbie Avatar asked Mar 28 '11 17:03

SharePoint Newbie


3 Answers

Maybe that is a Django pattern, but not a Python one by all means. That said, if the target audience of your module are Django developers, I would advise you to follow as much as possible the Django philosophy and its associated patterns.

like image 64
ubik Avatar answered Oct 10 '22 22:10

ubik


Django's ORM provides save() and delete() methods on the object. SQLAlchemy on the other hand has a so called session to which you add or delete objects.

Both are very popular so I'd say that both methods are about equal in terms of popularity. However in the context of a Django application going with the Django convention is probably preferable unless you have a good reason not to.

like image 33
DasIch Avatar answered Oct 10 '22 22:10

DasIch


Best of my recollection Django's models include save() and delete() methods so you can deal exclusively with objects, rather than interacting with a database connection object. I don't know that it's instantly a Python way of doing things, but I'm pretty sure it's a pervasive Django pattern.

If I was told "this is Django code" but the code diverged from how Django does things, that might be confusing.

like image 32
Kurt McKee Avatar answered Oct 10 '22 20:10

Kurt McKee