4

I'm having some problems with the super function 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()
b.f()

Then b.x will equal 12, but the function will output 'B', not 'A'. What I need is to execute A.p instead of B.p, how can I achieve that?

Thanks for your time :)


EDIT: Ok, I think you missed some details about my actual situation because of my poor example. Let's get to 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):
        # Notification objects "should" be created by Comment's __notify__
        Update.objects.create(
            object_id=self.id,
            target=self.receiver,
            type=self.__updatetype__(),
        )

    @classmethod
    @cache(prop_name='_notificationtype')
    def __notificationtype__(cls):
        return NotificationType.objects.get(name='USER_COMMENT')

    @classmethod
    @cache(prop_name='_updatetype')
    def __updatetype__(cls):
        return UpdateType.objects.get(name='USER_COMMENT')

    def retrieve_users(self):
        return [self.receiver]  # retrieve_users MUST return an iterable

The problem is with __insert__ and __notify__ methods on both models. __insert__ is a method that gets called the first time an object is recorded to the DB, and I use it for notification purposes mainly. Then, this is what I want to do:

  1. Create a UserComment object and save it
  2. Call UserComment instance's __insert__
  3. Call Comment's __insert__, which should call Comment's __notify__
  4. Call UserComment instance's __notify__ from __insert__

Is this possible in a more or less easy way or do I have to refactor my code?
Thanks again for all your answers.

cronos2
  • 288
  • 5
  • 13
  • 2
    The requirement looks a little funny to me. Why would you override p() if you didn't want to use it? – Rajesh J Advani Aug 15 '12 at 23:28
  • @RajeshJAdvani -- You can still use it, just not from the function `f`. – mgilson Aug 15 '12 at 23:43
  • 1
    Putting `x` as a class-attribute probably [can cause unexpected behaviour](http://stackoverflow.com/questions/206734/why-do-attribute-references-act-like-this-with-python-inheritance), you probably want to do `def __init__(self): self.x = 5` – dbr Aug 15 '12 at 23:55
  • @mgilson My point was, that if you are extending a class, then 'as a subclass' the behavior should be overridden. If a function in the same subclass doesn't want to use the overridden behavior, then the OO design probably wasn't done well. – Rajesh J Advani Aug 16 '12 at 02:47
  • @RajeshJAdvani yes, you're right, my example wasn't as clarifying as I thought at first. Will you please check the real code in my edit? – cronos2 Aug 16 '12 at 13:15
  • Not sure if I've understood your requirement correctly, but if `Comment.__notify__()` does something fundamentally different from `UserComment.__notify__()` then shouldn't the two methods be named differently rather than one overriding the other? Normally, if you were using polymorphism correctly, you would call `Comment.__notify__()` **from** `UserComment.__notify()` using super() – Rajesh J Advani Aug 16 '12 at 15:14
  • @RajeshJAdvani yeah, I realised it with aneroid answer, thank you very much for your help! – cronos2 Aug 17 '12 at 13:26

3 Answers3

5

You just say that you want to execute A.p():

class B(A):
    def p(self):
        print 'B'
    def super_p(self):
        return A.p(self) # prints 'A'

If you want to ensure that A.p() is exectuted, the best bet is to explicitly call it from your method in B. As Rajesh said, the best way to avoid this situation is to not override methods that you intend to call in the base class.

There's no way for the the superclass to know anything about the subclass. For example, if you instantiate the subclass B and it inherits a method x() and overrides a method p(), then when you call x(), that will call the p() definition in B, not the (overrridden) definition in A. This is exactly what you should expect - you've created a hybrid class and overridden one of the methods.

John Lyon
  • 11,180
  • 4
  • 36
  • 44
1

Remember that self refers to an instance and since b is an instance of B, self.p() refers to method p() in class B, not A. So either remove method p() from class B or explicitly call A's p() from B as:

class B(A):
    def p(self):
             super(B, self).p()
             print 'B'
    def f(self):
             super(B, self).f()
             self.x*=2

Or make p a staticmethod of A and call it from f() in class A with A.p(), especially if you don't need access to the class from that method making p a 'function' rather than a 'method':

class A(object):
    x=5
    @staticmethod
    def p():
        print 'A'
    def f(self):
        A.p()
        self.x+=1

With this B, the output is:

>>> b = B()
>>> b.f()
A
>>> b.x
12
>>> b.p()
A
B

Note how B got printed only in the b.p() call but not when it was b.f().

Community
  • 1
  • 1
aneroid
  • 12,983
  • 3
  • 36
  • 66
0

Watch the progression of events in your code:

self.x = 5

self.x = 6

self.x = 12

Given your code, that's the only way to get 12 as an output. If we were to run it from class A instead of B, though, the following behavior would emerge:

self.x = 5

self.x = 6

It seems to me that you want that behavior. The only difference is in the line self.x += 2.

This leads us to the more interesting question of why you wanted (or even expected) the child class to behave exactly its super's method after you overloaded the method to something different! A performs the function x+1, while B performs the function (x+1)*2, which is obviously different. The fundamental purpose of method overloading is to exhibit different behavior from the parent class, and lo and behold, it did!

Community
  • 1
  • 1
rsegal
  • 401
  • 3
  • 11
  • The point is that I want to execute the subclass method while maintaining parent's functionality. I think the code in the edit will be way more explicative than I am – cronos2 Aug 16 '12 at 13:23
  • So you want it to output `A` and then `12`? I haven't actually tried this, but what happens when you put `B.p`'s definition after `B.f`'s? That might cause it to find `A.f` instead of `B.f` as `self.f`. – rsegal Aug 16 '12 at 15:28