简体   繁体   中英

how can i run an abstract method in Python (child will implement it) and abstract hirrachy

I am complete new to Python , and i want to convert a Java project to Python, this is aa 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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM