-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
  • 37,241
  • 25
  • 195
  • 267
Rajeev
  • 44,985
  • 76
  • 186
  • 285

4 Answers4

4

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

emirc
  • 1,948
  • 1
  • 23
  • 38
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,939
  • 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
  • 89,107
  • 13
  • 149
  • 217
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
  • 29,419
  • 9
  • 72
  • 100