Dynamically adding methods and properties - Good or Bad?

14 views
Skip to first unread message

Aidas Bendoraitis

unread,
Dec 19, 2007, 5:19:49 PM12/19/07
to Django developers
Hello, Pythoners!

It is possible to add properties and methods to classes dynamically,
like this

{{{
def add_method():
def get_username(self):
return self.username
from django.contrib.auth.models import User
User.get_username = get_username

def call_added_method():
from django.contrib.auth.models import User
return User.objects.get(username="demo").get_username()

>>> add_method()
>>> call_added_method()
u'demo'
}}}

Let's say, I need an additional field which rather fits to the User
model than to the profile, i.e. previous_login which saves the date of
login which happened before current login (last_login).

I can add that field to the User model dynamically (for example, using
signals), instead of
1. adding that field to the profile where it makes no sense, or
2. copying and modifying the auth app and then rewriting all admin app
and other User-related parts of Django.

Do you consider this approach for extending existing code as a smart
python programming solution or an ugly hack?

Regards,
Aidas Bendoraitis aka Archatas

Jacob Kaplan-Moss

unread,
Dec 19, 2007, 8:35:21 PM12/19/07
to django-d...@googlegroups.com
Hi Aidas --

In the future, please direct questions of this nature to django-users.
Django-dev is for discussion of development *on* Django itself, not
for usage questions.

That said:

On 12/19/07, Aidas Bendoraitis <aidas.be...@gmail.com> wrote:
> Do you consider this approach for extending existing code as a smart
> python programming solution or an ugly hack?

We usually call this "monkeypatching", and as the name implies, most
sane developers consider it a bad idea. Wikipedia actually has a
pretty good summary of why this is:
http://en.wikipedia.org/wiki/Monkeypatch

Jacob

Aidas Bendoraitis

unread,
Dec 20, 2007, 7:10:52 AM12/20/07
to Django developers
Thanks for your answer, Jacob. I read the page in wikipedia and
actually I see some similar problems in overwriting third-party
classes (or using custom descriptors for third-party functions, or
using events, signals or hooks in programming) as in monkey-patching:
if a class changes, the custom extending class might not work as
expected.

I wrote this question in this group instead of the users' for a
reason. I think, that this kind of question is more philosophical than
educational, and this group consists of perfectionists who are able to
answer it properly. Also I didn't want to confuse the beginners with
the possibilities of Python, so that nobody would think that
monkeypatching is a good practice (until I evaluate that to myself,
how good or bad it is).

Regards,
Aidas Bendoraitis aka Archatas

P.S. Happy upcoming winter holidays, everyone!


On Dec 20, 2:35 am, "Jacob Kaplan-Moss" <jacob.kaplanm...@gmail.com>
wrote:
> Hi Aidas --
>
> In the future, please direct questions of this nature to django-users.
> Django-dev is for discussion of development *on* Django itself, not
> for usage questions.
>
> That said:
>
Reply all
Reply to author
Forward
0 new messages