Class Inheritance

Discussion in 'Python' started by Andrew Rekdal, Mar 13, 2008.

  1. 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
     
    Andrew Rekdal, Mar 13, 2008
    #1
    1. Advertising

  2. 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'
     
    Marc 'BlackJack' Rintsch, Mar 13, 2008
    #2
    1. Advertising

  3. 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
     
    Bruno Desthuilliers, Mar 13, 2008
    #3
    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. maxw_cc
    Replies:
    1
    Views:
    3,145
    Martijn van Steenbergen
    Dec 21, 2003
  2. E11
    Replies:
    1
    Views:
    4,783
    Thomas Weidenfeller
    Oct 12, 2005
  3. cppsks
    Replies:
    0
    Views:
    827
    cppsks
    Oct 27, 2004
  4. karthikbalaguru
    Replies:
    9
    Views:
    1,041
  5. johnsonlau
    Replies:
    1
    Views:
    777
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page