-2

In the following code C is inherited from A and B so when c.get() is get which classes get method is called can anyone explain this.....

class A:
  def get(self):
    print "In A get"

class B:
  def get(self):
    print "In B get"

class C(A,B):
  def __init__(self):
    print "In c init"

c=C()
c.get()
Cœur
  • 34,719
  • 24
  • 185
  • 251
Rajeev
  • 42,191
  • 76
  • 179
  • 280

4 Answers4

4

Well, it should be from class A. Because the search is depth-first, left-to-right.

emirc
  • 1,753
  • 1
  • 23
  • 36
1

First of all this code is inncorect as it does not have self variables within method declaration. Correct version is:

class A:
  def get(self):
    print "In A get"

class B:
  def get(self):
    print "In B get"

class C(A,B):
  def __init__(self):
    print "In c init"

c=C()
c.get()

Secondly this will print:

In c init
In A get

as ordering is defined in Method Resolution Order (MRO). Basicall class C will have all methods/attribues of B and then override by all mehods/attribues from A.

thedk
  • 1,869
  • 1
  • 20
  • 22
0

The error you mentioned is clear: your methods are intended to thake a reference to their bound-to object, but they don't.

Change them to

def get(self):

as they are methods.

glglgl
  • 85,390
  • 12
  • 140
  • 213
0

It'll be the A class method, like emirc say.

The error is given because you have to add self parameter to get definition, in that way:

class A:
 def get(self):
 print "In A get"

class B:
 def get(self):
 print "In B get"

class C(A,B):
 def __init__(self):
 print "In c init"

c=C()
c.get()
DonCallisto
  • 28,203
  • 8
  • 66
  • 94