I'm having issues with superfunctioning in Python. Suppose I have these two classes:
class A(object): x=5 def p(self): print 'A' def f(self): self.p() self.x+=1 class B(A): def p(self): print 'B' def f(self): super(B, self).f() self.x*=2 b = B() bf()
Then bx will be 12, but the function will output "B" and not "A". I need to execute Ap instead of Bp, how can I achieve this?
Thank you for your time:)
EDIT: Well, I think you missed some details about my real situation due to my bad example. Let me get the real code. I have these two classes (Django models):
class Comment(Insert, models.Model): content = models.TextField() sender = models.ForeignKey('logic.User') sent_on = models.DateTimeField(auto_now_add=True) def __insert__(self): self.__notify__() def __notify__(self): receivers = self.retrieve_users() notif_type = self.__notificationtype__() for user in receivers: Notification.objects.create( object_id=self.id, receiver=user, sender_id=self.sender_id, type=notif_type ) def __unicode__(self): return self.content class Meta: abstract = True class UserComment(Comment): is_reply = models.BooleanField() reply_to = models.ForeignKey('self', blank=True, null=True, related_name='replies') receiver = models.ForeignKey('User', related_name='comments') def __insert__(self): super(UserComment, self).__insert__() self.__notify__() def __notification__(self, notification): if self.is_reply: return '%s has replied your comment' % self.sender return super(UserComment, self).__notification__(notification) def __notify__(self):
The problem is the __insert__
and __notify__
on both models. __insert__
is the method that is called the first time the object is written to the database, and I use it for notification purposes mainly. Then this is what I want to do:
- Create a UserComment Object and Save It
- Invoking UserComment
__insert__
- Comment on
__insert__
, which should call Comment __notify__
- Calling UserComment
__notify__
from __insert__
Is this possible more or less easy or do I need to reorganize my code?
Thanks again for all your answers.
source share