0

I am complete new to Python , and i want to convert a Java project to Python, this is a a basic sample of my code in Java: (i truly want to know how to work with abstract classes and polymorphism in Python)

public abstract class AbstractGrandFather {

   protected ArrayList list = new ArrayList();

   protected AbstractGrandFather(){
          list.add(getGrandFatherName());
   }

   protected abstract String getGrandFatherName();

}

public abstract class AbstractParent extends AbstractGrandfather {

   protected AbstractParent(String name){
          super();
          list.add(name);
}

public class Child extends AbstractParent {

   public Child(String fatherName, String childName){
          super(fatherName);
          list.add(childName);
   }

   public String getGrandFatherName(){
          return "Samuel";
   }
}

This is what i tried to do in Python:

import abc
from abc import ABCMeta, abstractmethod

class AbstractGrandFather(object):
    __metaclass__ = ABCMeta

    @abc.abstractmethod
    def __init__(self):
        list = [self.get_command_name(self)]

    @abc.abstractmethod
    def get_command_name(self):
        pass

    @property
    def get_list(self):
        return self.list

class AbstractParent(AbstractGrandFather):
    __metaclass__ = ABCMeta

    @abc.abstractmethod
    def __init__(self, name):
        self.list = super.get_list.append(name)

    @abc.abstractmethod
    def get_command_name(self):
        pass

class Child(AbstractParent):
    def get_command_name(self):
        return "Samuel"

    def __init__(self, father_name, child_name):
        self.list = super(father_name).get_list.append(child_name)


x = Child("Dan," "Ben")

but it doesn't work and i get an error :

Traceback (most recent call last):
  File "Dummy.py", line 43, in <module>
    x = Child("Dan," "Ben")
TypeError: __init__() takes exactly 3 arguments (2 given)

Am I on the right track ? will appreciate some help and guiding lines.

Thank you

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
jezrael
  • 331
  • 3
  • 11
  • 3
    First, `super` doesn't work the exact same way in Python (among other things, because Python supports multiple inheritance). Also, we'd need to know the version of Python you're targetting, because Python 3 introduces some changes in `super`. Third... oh my... getters in Python. *shudder* – Ricardo Cárdenes Feb 13 '14 at 15:03
  • Im working on PyCharm, and the interpreter is Python 2.7.2 and the getter is only cause i don't know how to get to the "Grandfather" field "list" otherwise (for appending) will glad for a better solution.. – jezrael Feb 13 '14 at 15:22
  • 1
    Fix your line: `x = Child("Dan", "Ben")` – Ricardo Cárdenes Feb 13 '14 at 15:25

1 Answers1

1

Edit: fixed for multipython compatibility Edit2: added some pointers at the end...

Have a look at this:

class AbstractGrandFather(object):
    __metaclass__ = ABCMeta

    @abc.abstractmethod
    def __init__(self):
        self._list = [self.get_command_name()]

    @abc.abstractmethod
    def get_command_name(self):
        pass

    @property
    def list(self):
        return self._list


class AbstractParent(AbstractGrandFather):
    __metaclass__ = ABCMeta

    @abc.abstractmethod
    def __init__(self, name):
        super(AbstractParent, self).__init__()
        self.list.append(name)

    @abc.abstractmethod
    def get_command_name(self):
        pass


class Child(AbstractParent):
    def get_command_name(self):
        return "Samuel"

    def __init__(self, father_name, child_name):
        super(Child, self).__init__(father_name)
        self.list.append(child_name)

On Python 3 it's super() is enough, no need for super(class, instance).

You may want to read this about super.

Community
  • 1
  • 1
Ricardo Cárdenes
  • 9,004
  • 1
  • 21
  • 34
  • it still gives me: Traceback (most recent call last): File "Dummy.py", line 43, in x = Child("Dan," "Ben") TypeError: __init__() takes exactly 3 arguments (2 given) Im working on PyCharm, and the interpreter is Python 2.7.2 – jezrael Feb 13 '14 at 15:20
  • Works for me as is, with Python 2.7.5 and Python 3.3.2 – Ricardo Cárdenes Feb 13 '14 at 15:22
  • it was my bad , needed to change: x = Child("Dan", "Ben") You are the Best ! – jezrael Feb 13 '14 at 15:29
  • It was a bit unlucky on your side. Python `"..." "..."` works the same as `"..." + "..."`, so you were getting a totally unrelated error. – Ricardo Cárdenes Feb 13 '14 at 15:32