Rajeev
Rajeev

Reputation: 46959

Inheritence and polymorphism properties

In the following code i am trying to implement inheritance and polymorphism properties,My question is obj1.hello3("1param","2param") obj1.hello3("11param") are these statements not correct what would be the correct way of doing this

  #!/usr/bin/python

  class test1:
     c,d = ""
     def __init__(self,n):
        self.name = n
        print self.name+"==In a"
     def hello1(self):
        print "Hello1"
     def hello3(self,a,b):
        #print "Hello3 2 param"+str(a)+str(b)
        #print "ab"+str(self.a)+str(self.b)+"Hello1"
        print "Hello3 2 param"

     def hello3(self,a):
        #print "a"+str(self.a)+"Hello1"
        print "Hello3 1 param"+str(a)

  class test2(test1):
     def __init__(self,b):
        test1.__init__(self, "new")
        self.newname = b
        print self.newname+"==In b"
     def hello2(self):
        print "Hello2"

  obj= test1("aaaa")
  obj1=test2("bbbb")
  obj1.hello1()
  obj1.hello2()
  obj1.hello3("1param","2param")
  obj1.hello3("11param")

Upvotes: 1

Views: 307

Answers (4)

HongboZhu
HongboZhu

Reputation: 4532

You can use *args, or **kwargs. See examples and explanations

class test1:
    def __init__(self):
        print 'init'
    def hello(self, *args):
        if len(args) == 0:
            print 'hello there'
        elif len(args) == 1:
            print 'hello there 1:',args[0]
        elif len(args) == 2:
            print 'hello there 2:',args[0],args[1]

class test2:
    def __init__(self):
        print 'init'
    def hello(self, **kwargs):
        if len(kwargs) == 0:
            print 'hello there'
        elif 'a' in kwargs and 'b' not in kwargs:
            print 'hello there a:', kwargs['a']
        elif 'a' in kwargs and 'b' in kwargs:
            print 'hello there a and b:', kwargs['a'], kwargs['b']

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613382

You are trying to implement method overloading rather than inheritance and polymorphism.

Python does not support overloading in the way that C++, Java, C# etc. do. Instead, to achieve what you want in Python, you need to use optional parameters.

def hello3(self,a,b=None):
    if b is None:
        print "Hello3 1 param", a
    else:
        print "Hello3 2 param", a, b

...

obj1.hello3("a")#passes None for b param
obj1.hello3("a", "b")

Upvotes: 5

Bogdan
Bogdan

Reputation: 8246

Well first of all you have some coding problems like: c,d = "" or that random sel there or aa.__init__(self, "new").

I don't know if this is from fast typing or this is your actual code. In test2 __init__ method the correct call is test1.__init__(self, "new").

Also as coding style you should write your classes with camelcase starting with a capitalize letter, so: Test1, MyNewClass.

The calls are correct, however python does not support overloading in the way java does for example. So the multiple def hello3(... should give you a duplicate signature

Upvotes: 0

Roman Bodnarchuk
Roman Bodnarchuk

Reputation: 29737

Python doesn't have method overloading, so

 def hello3(self,a):
    #print "a"+str(self.a)+"Hello1"
    print "Hello3 1 param"+str(a)

just replaced above definition of hello3 method. Also note that all methods in Python classes are 'virtual' in term of C++, so polymorphism is always here.

Also by line aa.__init__(self, "new") you probable mean test1.__init__(self, "new").

Upvotes: 1

Related Questions