[Tutor] Inheritance
Alan Gauld
alan.gauld at blueyonder.co.uk
Wed Dec 17 03:17:54 EST 2003
> One question is if class A inherits from class B and C. And
then B inherits
> from M and N. And C inherits from F and G, while F and N
inherit from class
> z which inherits from object, what is the name resolution.
>
>
>
> Z
> M N F G
> B C
> A
>
A,B,M,N,Z,C,F,Z,G
I think.
Python works left to right in declaration order. And it stops
with the first successful find.
> Also can someone explain to me what super() does,
Sorry, its a new feature and I've not got round to using it
yet. I just tried a wee experiment and it didn't work how
I expected so I need to go rad the docs I think! :-)
> Next using the same classes as above, if z.__init__: connects
to a database,
> how can I make sure that it is only called once,
You could test for the existence of the connection in the
init()... But it looks a bit grubby, thee might be a better
way someone else can think of...
class Z:
def __init__(self,Database):
try:
if self.db: pass
except AttributeError:
self.db = connect(Database)
> class test:
> def __init__():
> self.x = 'hello'
> print self.x
>
>
> Tha above code raises an error. What can I do to make it work??
Add a self parameter to the init method
def __init__(self):
HTH,
Alan G.
More information about the Tutor
mailing list