Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Class Inheritance

Reply
Thread Tools

Class Inheritance

 
 
Andrew Rekdal
Guest
Posts: n/a
 
      03-13-2008
I am trying to bring functions to a class by inheritance... for instance in
layout_ext I have..


--- layout_ext.py---------
class Layout()
def...some function that rely on css in Layout.py
def...

---EOF--

in the main application file I have...
----Layout.py---
from layout_ext import Layout
from CSS import CSS
css = CSS()
class Layout(Layout)
def __init__
more code.....

----EOF----


Problem is layout_ext and Layout code is dependant on a Class instance
'css'. Whenever the CSS instance it parses a file, this means that I would
have to parse the file twice?? Why is this? Can I do something like pass an
already created instance to the import?


-- Andrew


 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      03-13-2008
On Thu, 13 Mar 2008 00:06:52 -0500, "Andrew Rekdal" < wrote:

> Problem is layout_ext and Layout code is dependant on a Class instance
> 'css'.


Then pass that instance to the `Layout` class in the `__init__()` so both,
the base class and the subclass use the same `CSS` instance.

Ciao,
Marc 'BlackJack'
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      03-13-2008
Andrew Rekdal < a écrit :
> I am trying to bring functions to a class by inheritance... for instance in
> layout_ext I have..
>
>
> --- layout_ext.py---------
> class Layout()
> def...some function that rely on css in Layout.py


It shouldn't, definitively. The Layout instance should have a reference
on the CSS instance, ie:

# layout_ext.py
class LayoutExt(object):
def __init__(self, css):
self.css = css

def some_function(self):
do_something_with(self.css)

# layout.py
from layout_ext import LayoutExt
from CSS import CSS

class Layout(LayoutExt):
def __init__(self, css):
LayoutExt.__init__(self, css)
# etc


> def...
>
> ---EOF--
>
> in the main application file I have...
> ----Layout.py---
> from layout_ext import Layout
> from CSS import CSS
> css = CSS()
> class Layout(Layout)


You will have a problem here - this class statement will shadow the
Layout class imported from layout_ext. Remember that in Python, def and
class statements are executed at runtime and that they bind names in
current namespace - here, the 'class Layout' statement rebinds the name
'Layout' in the Layout module's namespace.


> def __init__
> more code.....
>
> ----EOF----
>
>
> Problem is layout_ext and Layout code is dependant on a Class instance
> 'css'. Whenever the CSS instance it parses a file, this means that I would
> have to parse the file twice?? Why is this? Can I do something like pass an
> already created instance to the import?


Wrong solution, obviously. cf above for the most probably correct one.

HTH
 
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
C++ Struct inheritance against class inheritance johnsonlau C++ 1 07-21-2008 04:58 PM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57