Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: inherit without calling parent class constructor?

Reply
Thread Tools

Re: inherit without calling parent class constructor?

 
 
Christian Dieterich
Guest
Posts: n/a
 
      01-26-2005
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

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      01-26-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Query regarding calling parent's class method which is overridden in child class??? parkarumesh@gmail.com Java 2 04-26-2007 09:34 PM
Query regarding calling parent's class method which is overridden in child class??? parkarumesh@gmail.com Java 0 04-26-2007 09:24 AM
inherit without calling parent class constructor? Christian Dieterich Python 3 01-26-2005 11:09 PM
Re: inherit without calling parent class constructor? Christian Dieterich Python 1 01-26-2005 11:02 PM



Advertisments