Amit Pal
Amit Pal

Reputation: 11052

How to return a value computed by overriding method from a subclass to it's base class method in python

I have a base class like this:

class base(object):
    name = ""
    def __init__(self,name):
        self.user =user
    def context(self,value):
        return {}

Instead of this i created an another subclass and override this above "context" method:

class override(base):
    name = "New class"
    def context(self, value):
        a = []
        a = "hello","b"
        return a

I want to create a plugins, all they have are different definitions (different value of list will return) for context method. So i want to do this by putting override functionality.

When i call this base.context function from an another file. Following things should be happen:

  1. The list i.e "a" (in override context method) should return it's value to base context method first.

  2. After getting a value from override.context method, base.context will return that value to from where it's called after making some changes into it.

But, When i call this base.description method it will return a value directly from override.description method. So i am not able to make that changes into it :(

Upvotes: 0

Views: 408

Answers (2)

aquavitae
aquavitae

Reputation: 19114

A few things in you question are a little confusing, but I think that what you want is something like this example (python2.7):

class base(object):
    name = ""

    def __init__(self, name):
        self.user =user

    def context(self, value):
        return [value]


class override(base):
    name = "override"

    def context(self, value):
        base_context = super(override, self).context(value)
        a = ["hello", "a"] + base_context

    def description(self):
        return "override description"

The you can do the following:

>>> a = override()
>>> override.context("value")
["hello", "a", "value"]
>>> override.description()
"override description"

However, calling these methods on base gives different results:

>>> a = base()
>>> a.context("value")
["value"]
>>> base.description()
Traceback (most recent call last):
    ...
AttributeError: 'base' has no attribute 'description'

Its not entirely clear if this is what you want though, because you refer to base.context getting a value from override.context, whereas it normally happens the other way around. The same goes for override.description. If you really want base.context to get a value from override.context this can be done through base.__subclasses__(), but you would have to deal with all subclasses of base, and this sort of usage is extremely uncommon (and usually undesirable).

Upvotes: 3

San4ez
San4ez

Reputation: 8241

Use super(override, self).context(value) to get context from base class.

But you example is a bit confusing because:

1) base.context returns dict

2) variable a in override.context is set to list, then to tuple, so override.context returns tuple

Upvotes: 2

Related Questions