java - Python abstract property with method access detection -


i'm translating java source code python. have abstract java class translate in:

from abc import abcmeta, abstractmethod  class myabstractclass(metaclass=abcmeta):     @property     @abstractmethod     def sampleprop(self):         return self.sampleprop      # 1 important     @property     @abstractmethod     def samplelist(self):         return self.samplelist 

my doubt child classes in java add elements list in child classes because list (java.util.list in java) initialized in parent java class member.

public myabstractclass {     list<object> samplelist = new arraylist<object>(); }  public chidclass extends myabstractclass {     public childclass() {         samplelist.add("element 1")         samplelist.add("element 2")     } } 

parent class lacking constructor, if implement constructor init in python initialize list, have write super() in child classes.

i don't know if should that. thought maybe doing work:

class myabstractclass(metaclass=abcmeta):     @property     @abstractmethod     def samplelist(self):         if not self.samplelist:         # alternative way         #if not hasattr(self, 'samplelist'):             self.samplelist = []         return self.samplelist 

i'm going try if works, know alternatives , best , safest way this. i'm looking engineering way if changes, consequences in code minimal

thanks

the correct way create list in __init__ function.

any correctly implemented child class call parent class's constructor, regardless of whether constructor exists or not (i.e. it's default object constructor), have no reason avoid creating __init__ method.

class myabstractclass(metaclass=abcmeta):     def __init__(self):         self.samplelist = []      @property     @abstractmethod     def sampleprop(self):         return self.sampleprop 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -