Multiple Inheritence and data attributes

D

Derek Basch

Hello Everyone,

Given:

class A:
def __init__(self):
super(A, self).__init__()
self.dog = "fluffy"
def changeDog(self):
self.dog = "spike"

class B:
def __init__(self):
super(B, self).__init__()

class C(object, A, B):
def __init__(self):
super(C, self).__init__()
def printDog(self, cls):
print cls.dog

c = C()
c.printDog(c)

How can I access data attributes of superclasses? I can see the
function attributes of the superclasses:

['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__',
'changeDog', 'printDog']

but not the data attributes. Shouldn't the derived class have that data
attributes of superclasses? I guess I am not understanding the python
implementation.

Thanks,
Derek
 
W

wittempj

Two things:
- A call to super doesn't make senseif a class is not derived, and
class b seems superfuous.
- Code below is a working example of your code, the way you did it it
generates an error.

-#!/usr/bin/env python
-class A(object):
- def __init__(self):
- super(A, self).__init__()
- self.dog = "fluffy"
- def changeDog(self):
- self.dog = "spike"

-class B(object):
- def __init__(self):
- super(B, self).__init__()

-class C(A, B):
- def __init__(self):
- super(C, self).__init__()
- def printDog(self, cls):
- print cls.dog

-c = C()
-c.printDog(c)
-c.changeDog()
-c.printDog(c)

martin@ubuntu:~$ ./test.py
fluffy
spike
martin@ubuntu:~$
 

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

Similar Threads

object().__dict__ 0
Private variables 0
newstyle classes and __getattribute__ 5
Where does a class closure live? 0
ctypes help 6
property () for Java Programmers ? 2
Issues with writing pytest 0
PS. 0

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top