1

在这个幼稚的代码示例中,我遇到的问题是,每次调用 .do() 时,都需要处理一个条件列表,以使类按预期运行。这不是很有效,我很确定还有另一种方法,但我不能指望它。

我可以采取什么方法使这个类的行为更加相同,但以更有效的方式?

class translate():
    def __init__(self, EN=True, FR=False, DE=False, SP=False):
        self.EN=EN
        self.FR=FR
        self.DE=DE
        self.SP=SP

    def do(self, word):
        if self.EN:
            self.en(word)
        if self.FR:
            self.fr(word)
        if self.DE:
            self.de(word)
        if self.SP:
            self.sp(word)

    def en(self, word):
        print "In English: %s"%(word)

    def fr(self, word):
        print "In French: %s"%(word)

    def de(self, word):
        print "In German: %s"%(word)    

    def sp(self, word):
        print "In Spanish: %s"%(word)

tr=translate(FR=True)
tr.do("blah")

我可以这样做,但我只能做一种语言:

    class translate():
    def __init__(self, EN=False, FR=False, DE=False, SP=False):
        if EN:
            self.do=self.en
        elif FR:
            self.do=self.fr
        elif DE:
            self.do=self.de
        elif SP:
            self.do=self.sp
        else:
            self.do=self.unkown

    def en(self, word):
        print "In English: %s"%(word)

    def fr(self, word):
        print "In French: %s"%(word)

    def de(self, word):
        print "In German: %s"%(word)    

    def sp(self, word):
        print "In Spanish: %s"%(word)

    def unknown(self, word):
        print "Unknown: %s"%(word)

tr=translate(FR=True)
tr.do("blah")
4

2 回答 2

4

在您的原始代码中,do可以调用多个翻译方法。因此,我们应该跟踪一个列表(或者可能是一组)翻译方法:

class Translate():
    def __init__(self, EN=True, FR=False, DE=False, SP=False):
        self.translators = [method for lang, method in
                            zip((EN, FR, DE, SP),
                                (self.en, self.fr, self.de, self.sp))
                            if lang]

    def do(self, word):
        for method in self.translators:
            method(word)

    def en(self, word):
        print "In English: %s" % (word)

    def fr(self, word):
        print "In French: %s" % (word)

    def de(self, word):
        print "In German: %s" % (word)

    def sp(self, word):
        print "In Spanish: %s" % (word)

tr = Translate(FR=True)
tr.do("blah")

产量

In English: blah
In French: blah
于 2013-01-16T21:06:21.297 回答
0

您实际上并没有在任何地方存储翻译,因此您只需在为对象初始化的每种语言输入单词时按拼写打印单词。您需要一个系统来存储和检索翻译,并为您提供为给定单词设置翻译的所有值。这会起作用:

class WordTranslationMap(dict):

    def __init__(self):
        dict.__init__(self, languages)
        for language in languages:
            self[language] = None

    def all(self):
        for language, translation in self.iteritems():
            if translation is not None:
               yield (language, translation)

class TranslationDictionary(dict):

    def __init__(supported_languages=['EN', 'SP', 'FR', 'DE']):
        dict.__init__(self)
        self.supported_languages = supported_languages

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            word = WordTranslationMap(self.supported_languages)
            self[key] = word
            return word

words = TranslationDictionary()
words['hello']['EN'] = 'hello'
words['hello']['FR'] = 'bonjour'
for translation in words['hello'].all:
     print translation

或者,先按语言组织:

class WordToken(object):
    def __init__(value, translation_dictionary, word_id=None):
        self.value = value
        self.word_id = translation_dictionary.next_id.next() if word_id is None else word_id
        self.translation_dictionary= translation_dictionary

    def __getitem__(self, key):
        try:
            return self.translation_dictionary[key][self.word_id]
        except KeyError:
            return None

    def __setitem__(self, key, value):
        self.translation_dictionary[key][self.word_id] = value

    def all(self):
        for language, translations in self.translation_dictionary.language_dictionaries.iteritems():
            try:
                yield (language, str(translations[self.word_id]))
            except KeyError:
                pass

    def __str__(self):
        return self.value

class LanguageDictionary(dict):

    def __init__(self, translation_dictionary):
        dict.__init__(self)
        self.translation_dictionary = translation_dictionary

    def add_word(self, word):
        word = WordToken(word, self.translation_dictionary)
        self[word.word_id] = word

    def __setitem__(self, key, value):
        try:
            if value.word_id != key:
                raise ValueError('Tried to set translation to word object that does not match language dictionary key.')
        except AttributeError:
            value = WordToken(value, self.translation_dictionary, key)
        dict.__setitem__(self, key, value)

class TranslationDictionary(object):

    def __init__(self):
        self.next_id = itertools.count(start=0, step=1)
        self.language_dictionaries = defaultdict(LanguageDictionary)

    def __getitem__(self, key):
        return self.language_dictionaries[key]

dictionary = TranslationDictionary()
dictionary['EN'].add_word('hello')
dictionary['EN']['hello']['FR'] = 'bonjour'
for translation in dictionary['EN']['hello'].all():
    print translation
于 2013-01-16T21:18:44.780 回答