Skip to content

Instantly share code, notes, and snippets.

@k4ml
Created January 29, 2014 23:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save k4ml/8699461 to your computer and use it in GitHub Desktop.
Save k4ml/8699461 to your computer and use it in GitHub Desktop.
POPO - Plain Old Python Object
class Messaging(object):
def __init__(self, user, sender, receiver, message):
self.user = user
self.sender = sender
self.receiver = receiver
self.message = message
def send(self):
required_points = self.get_required_points()
if required_points > self.user.points:
raise Exception('Not enough points')
return self.backend.send(self.sender, self.receiver, self.message)
@k4ml
Copy link
Author

k4ml commented Jan 31, 2014

For some reason one of my comment here was lost and I'm not in the mood of retyping it again. Basically the class is what being referred as domain layer in languages such as Java or C#. The class should be plain python object (POPO) that does not contain any dependencies to the framework (in theory basically, in practice we still has to couple it at least to the django ORM). This mean it possible for you to take the class out of your django project and still being able to instantiate it by just passing it's required parameters.

Example of such class - gist.github.com/k4ml/8699461.

@gcaraciolo
Copy link

hi @k4ml, I'm new to django/python and I'm looking for articles, books, etc, about the strategy of using pure python objects to place complex business logic. My biggest difficult now is about django project structures. Where this kind of classes should be placed?

do you have any sources related to this?

@khodemehran
Copy link

hi @k4ml, I'm new to django/python and I'm looking for articles, books, etc, about the strategy of using pure python objects to place complex business logic. My biggest difficult now is about django project structures. Where this kind of classes should be placed?

do you have any sources related to this?

hi gcaraciolo
Service objects are plain old Python objects (POPOs) that encapsulate a
service or interactions with a system. They are usually kept in a separate
file named services.py or utils.py.

@gabrielsimas
Copy link

I'm in 2024 and enjoying this thread. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment