简体   繁体   English

Python中的动态类实例命名

[英]Dynamic class instance naming in Python

I'm trying to write a program that determines whether two words are cognates. 我正在尝试编写一个程序,以确定两个单词是否相同。 I've written two classes: featTup (basically a wrapper around a tuple containing the values of a letter), and featWord (basically a wrapper around of featTup objects.) 我编写了两个类:featTup(基本上是包含字母值的元组的包装器)和featWord(基本上是featTup对象的包装器)。

(Sorry this is all so long!) (很抱歉,这么长!)

Here's some (hopefully relevant) code: 这是一些(希望是相关的)代码:

class featTup(object):
    def __init__(self,char):
        self.char = char
        self.phone_vals = None
        self.dia_vals = None
        if self.char in phone_codes:
            self.phone_vals = phone_feats[phone_codes.index(char)]
        elif self.char in dia_codes:
            self.dia_vals = dia_feats[dia_codes.index(char)]   
        ...


class featWord(list):
    def do_dia(self,char_feats,dia_feats):
        #This method handles the changes diacritics make to preceding phones
        for val in dia_feats:
            if dia_val:
                char_feats.change_val(tup,char_feats.index(dia_val),dia_val)

    def get_featWord(self):
        return self.word_as_feats

    def __init__(self,word):
        self.word = word
        self.word_as_feats = [featTup(char) for char in self.word]
        for char in self.word_as_feats:
            if char.is_dia():
                i = self.word_as_feats.char_index(char)
                self.word_as_feats.do_dia(self.word_as_feats[i-1],self.word_as_feats[i])

    def word_len(self):
        return len(self.get_featWord())

    def char_index(self,char):
        return self.word_as_feats.index(char)

The issue is that I want to take a list of words and make featWord objects for all of them. 问题是我想列出一个单词列表,并为所有单词制作featWord对象。 I don't know how long each list will be, nor do I know how many characters will be in each word. 我不知道每个列表要多长时间,也不知道每个单词中有多少个字符。 More code: 更多代码:

def get_words(text1,text2):
    import codecs
    textin1 = codecs.open(text1,encoding='utf8')
    word_list1 = textin1.readlines()
    textin1.close()
    textin2 = codecs.open(text2,encoding='utf8')
    word_list2 = textin2.readlines()
    textin2.close()
    print word_list1,word_list2
    fixed_words1 = []
    fixed_words2 = []
    for word in word_list1:
        fixed_word = word.replace('\n','')
        fixed_words1.append(fixed_word)
    for word in word_list2:
        fixed_word = word.replace('\n','')
        fixed_words2.append(fixed_word)
    print fixed_words1,fixed_words2
    words1 = [(featWord(word)) for word in fixed_words1]
    words2 = [(featWord(word)) for word in fixed_words2]
    # for word1 in fixed_words1:
        # for x in xrange(len(fixed_words1)):
        words1.append(featWord(word))
    for word2 in fixed_words2:
        #for x in xrange(len(fixed_words2)):
        words2.append(featWord(word))
    print words1
    #words1 = [featWord(word) for word in fixed_words1]
    #words2 = [featWord(word) for word in fixed_words2]
    return words1,words2

def get_cog_dict(text1,text2,threshold=10,print_results=True):
    #This is the final method, running are_cog over all words in
    #both lists.
    word_list1,word_list2 = get_words(text1,text2)
    print word_list1, word_list2

As it stands, when I call either of these last two methods, I get lists of empty lists; 就目前而言,当我调用这最后两个方法中的任何一个时,都会得到一个空列表。 when I instantiate new featWord objects from strings I just give it (eg x = featWord("ten"), or whatever) it works fine. 当我从字符串实例化新的featWord对象时,我只是给它(例如x = featWord(“ ten”)或其他),它可以正常工作。 A relevant thing is that featWord seems to return an empty list instead of (when I instantiate featWord from IDLE, as above, it comes back as a list of featTup instances, which is good). 一个相关的事情是featWord似乎返回了一个空列表,而不是返回一个空列表(当我从IDLE实例化featWord时,如上所述,它作为featTup实例的列表返回了,这很好)。 I'm not sure why/if that's the problem. 我不确定为什么/如果那是问题。

It seems to me that (at least part of) my problem stems from improperly initializing the featWord. 在我看来,(至少部分)我的问题源于对featWord的不正确初始化。 I'm constructing them, or whatever, but not assigning them names. 我正在构造它们,或其他任何东西,但未分配它们的名称。 I've tried just about everything I can think of (as the commented-out sections prove), and I'm stumped. 我已经尝试了几乎所有我能想到的东西(正如注释部分所证明的那样),但我感到很困惑。 There're answers on here about using dictionaries to name class instances and such, but since I can't pre-define a dictionary (each word and wordlist is potentially a different length), I'm not sure what to do. 关于使用字典命名类实例等的答案,但是由于我无法预定义字典(每个单词和单词列表的长度可能不同),所以我不确定该怎么做。

Any help would be GREATLY appreciated. 任何帮助将不胜感激。 I'm kind of driving myself insane over here. 我有点疯了。 Thanks. 谢谢。

your featWord class derives from list , but you never append anything to self , and you have overridden __init__ , so lists __init__ never gets called, too. 您的featWord类是从list派生的,但是您绝不会在self featWord添加任何内容,并且您已经覆盖了__init__ ,因此也永远不会调用列表__init__

So a featWord instance is just an empty list with some attributes and methods. 因此, featWord实例只是一个包含一些属性和方法的空列表。

Their __repr__ is list 's __repr__ , that's why a list of featwords displays as a list of empty lists. 他们的__repr__list__repr__ ,这就是为什么功能词列表显示为空列表的原因。

So: implement a meaningful __repr__ , do not subclass from list , append something meaninful to self . 因此:实现有意义的__repr__ ,不要从list继承子类,对self有意义的东西。 Any of that will solve your problem. 任何一种都可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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