Class Inheritance

A

Andrew Rekdal

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
 
M

Marc 'BlackJack' Rintsch

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'
 
B

Bruno Desthuilliers

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top