mercredi 19 octobre 2016

python create instance with different methods [on hold]

I am wondering how to construct an instance of a class with different methods than the ones defined in the class ? For example, I define a language translator which has to read an input, parse it, process the text and finally to render the result :

class Translator():
    # Constructors:
    def __init__(self, *args, **kwargs):
         #default constructor
    @classmethod
    def from_url(self, *args, **kwargs):
         # GET url
         # parse text
    @classmethod
    def from_clipboard(self, *args, **kwargs):
         # read clipboard
         # etc.
    # Translator
    def translate(self, *args, **kwargs):
         # translate
    # Renderers
    def render(self, *args, **kwargs):
         # render

Sometimes I would like to translate or render differently than what is defined in Translator. Is there a clean way to define a different method translate or render or even from_other_source to change the behaviour of the object ? Can @classmethod be used this way ?

Thanks for any help

Aucun commentaire:

Enregistrer un commentaire