Re: inherit without calling parent class constructor?

Discussion in 'Python' started by Christian Dieterich, Jan 26, 2005.

  1. On Dé Céadaoin, Ean 26, 2005, at 11:46 America/Chicago, Steven Bethard
    wrote:

    > I'm confused as to how you can tell when it's avoidable... Do you
    > mean you don't want to call 'method' if you don't have to? Could you
    > make size a property, e.g.


    > Then 'size' won't be calculated until you actually use it. If 'size'
    > is only to be calculated once, you might also look at Scott David
    > Daniels's lazy property recipe:


    On Dé Céadaoin, Ean 26, 2005, at 12:03 America/Chicago, Daniel Dittmar
    wrote:

    > - rename B to A
    > - class B (A)
    > - move the costly constructor from A to B
    > - class D (A)
    >
    > You can now move some parts from B.__init__ to A.__init__ if they are
    > really needed by D as well.


    Thanks for the input.

    Yes, I see. I should have been more specific about 'when it's
    avoidable'. I guess this was part of my question: Is it avoidable?

    Steven, your solution works fine, as long as I don't access the size
    attribute. However,
    my instances of A use their size attributes quite often within other
    methods of A (not listed in my example). So, I'd just postpone the
    execution of the expensive part until later.

    Daniel, maybe I should have made it clearer in my example. The
    "A"-objects actually need the size attribute. If I'd outsource the size
    attribute (and its computation) to a class I don't inherit from, the
    "A"-objects won't have access to it. Or did I miss something in your
    solution?

    The size attribute only needs to be computed once and stays constant
    after that. The lazy property recipe of Scott David Daniels looks
    promising. I'll try that, when I've installed Python 2.4. However, I
    need my package to work on machines where there is Python 2.2 and 2.3
    only.

    Thanks for more ideas,

    Christian
     
    Christian Dieterich, Jan 26, 2005
    #1
    1. Advertising

  2. Christian Dieterich wrote:
    > The size attribute only needs to be computed once and stays constant
    > after that. The lazy property recipe of Scott David Daniels looks
    > promising. I'll try that, when I've installed Python 2.4. However, I
    > need my package to work on machines where there is Python 2.2 and 2.3 only.


    Note that:
    @deco
    def func(...):
    ...
    is basically just syntactic sugar for:
    def func(...):
    ...
    func = deco(func)
    So you don't need 2.4 to use the recipe; you just need a version of
    Python that supports descriptors (2.2 or later) since the recipe uses a
    class with __get__. I believe the following code should work for Python
    2.2 and later, though I only have 2.4 to test it on at home.

    py> class LazyAttribute(object):
    .... def __init__(self, calculate_function):
    .... self._calculate = calculate_function
    .... def __get__(self, obj, _=None):
    .... if obj is None:
    .... return self
    .... value = self._calculate(obj)
    .... setattr(obj, self._calculate.func_name, value)
    .... return value
    ....
    py> class B(object):
    .... def __init__(self, length):
    .... self._length = length
    .... def size(self):
    .... print 'some expensive calculation'
    .... return self._length
    .... size = LazyAttribute(size)
    ....
    py> class D(B):
    .... def __init__(self, length):
    .... super(D, self).__init__(length)
    .... self.value = 1
    ....
    py> b = B(5)
    py> b.size
    some expensive calculation
    5
    py> b.size
    5
    py> d = D(6)
    py> d.size
    some expensive calculation
    6
    py> d.size
    6

    Note that b.size and d.size are only calculated once each, and if d.size
    is never accessed, you'll never incur the costs of calculating it.

    Steve
     
    Steven Bethard, Jan 26, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pantalaimon
    Replies:
    3
    Views:
    51,111
    John Harrison
    Oct 9, 2004
  2. Christian Dieterich

    inherit without calling parent class constructor?

    Christian Dieterich, Jan 26, 2005, in forum: Python
    Replies:
    3
    Views:
    414
    Jeff Shannon
    Jan 26, 2005
  3. Christian Dieterich
    Replies:
    1
    Views:
    376
    Steven Bethard
    Jan 26, 2005
  4. ali
    Replies:
    4
    Views:
    590
    David Harmon
    Mar 5, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,287
Loading...

Share This Page