0

私はPythonに比較的慣れていないので、クラスを始めたばかりです。私は、正しくPythonicな方法で解決したいという少し複雑なものを手にしています。

「本」などのクラスが欲しいです。このクラスで、構造の異なる 2 つの Python 辞書、たとえば「化学」と「(英語) 辞書」を処理するようにします。これら 2 つの python 辞書に対して、「 find」、「add」、「remove」、「list」などの異なる構造のアクションを実行できるようにしたいと考えています。

これら 2 つの構造、「化学」と「辞書」は異なるため、「追加」、「削除」、および「検索」関数は異なるコード構造を持つ必要があります。そのため、「化学」で何かを見つけている場合、実行されるコード ブロックは「辞書」での「検索」とは異なります。

私の質問:

  • このクラスをどのように構成すればよいですか?

  • どのように電話をかければよいですか?

  • 最終的には、関数呼び出しが と のようになったら最高books.chemistry.find('keyword to find')ですbooks.dictionary.find('other keyword to find')。これは可能ですか?どうすればこのように入手できますか?

ありがとうございました。

4

2 に答える 2

1

通常、クラス間でいくつかのメソッドを共有したいのでBook、一般的なプロパティを持つクラスを作成してから、さまざまなメソッドを定義しChemistry、からプロパティまたはメソッドを継承するクラスを作成できます。EnglishfindBook

class Books(object):
    def __init__(self, dictionary):
        self.input = dictionary
    def commonMethod(self):
        print 'This is a shared method'

class Chemistry(Books):
    def find(self):
        print 'This is a particular method'

class English(Books):
    def find(self):
        print 'This is other particular method'

chemistryBook = Chemistry({'hello': 'goodbye'})
chemistryBook.find()
# This is a particular method

EnglishBook = English({'hello': 'goodbye'})
EnglishBook.find()
# This is other particular method

アップデート

あなたのメッセージの最後の部分を読んでいません。多分これはあなたが望むものです:

class Books(object):
    def __init__(self, dictionary):
        self.input = dictionary
        if len(dictionary) > 1:
            print 'More than 1'
            self.command = Chemistry(self.input)
        else:
            print 'Less or equal'
            self.command = English(self.input)

class Chemistry(object):
    def __init__(self, d):
        self.d = d
    def find(self):
        print "Now you can manipulate your dictionary in Chemistry", self.d

class English(object):
    def __init__(self, d):
        self.d = d
    def find(self):
        print "Now you can manipulate your dictionary in English", self.d


book = Books({'hello': 'goodbye'})
book.command.find()
# Less or equal
# Now you can manipulate your dictionary in English {'hello': 'goodbye'}

book2 = Books({'hello': 'goodbye', 'one': 1})
book2.command.find()
# More than 1
# Now you can manipulate your dictionary in Chemistry {'hello': 'goodbye', 'one': 1}

基本的に、これは入力に応じて必要なクラスの特定のインスタンスを作成します。この場合、引数として渡す辞書の長さが 1 より大きい場合、Chemistry() のインスタンスが作成されます。それ以外の場合は、English() のインスタンスを作成します。その後、find メソッドを使用できます。

于 2012-10-25T22:50:08.557 に答える
1
#Here's what I would do.

class Page(object):
    def __init__(self, text):
        self.text = text

class Book(object):

    def __init__(self, pages):

        if type(pages) == Page:
            self.pages = [pages]
        elif type(pages) == list:
            self.pages = pages

    def find(self, term):

        for page in self.pages:
            if term in page.text:
                return True
        return False

class ChemistryBook(Book):

    def __init__(self, pages):

        super(ChemistryBook, self).__init__(pages)

    #def someChemistryBookSpecificMethod(self):
    pass

if __name__ == '__main__':

    page = Page("Antoine Lavoisierb")
    chemBook = ChemistryBook(page)
    print chemBook.find("Antoine")
于 2012-10-25T22:50:41.143 に答える