Meta Class
Hi; I think I have a good candidate for a meta class here. Never done this before and would like someone to help. In the code that follows, there is one variable that needs to be changed: the letter 'a' as inserted in construction of the variable 'word'. In other applications, I will need to change that to two variables, but they are independent within this code. How do I go about abstracting these variables to make a meta class? TIA, Trevor >>> def testing(): ... for word in wordPool: ... i = 0 ... i2 = 0 ... i3 = [] ... new = [] ... for letter in word: ... if letter == 'a': ... i3.append(i2) ... i2 += 1 ... new.append(word[0:i + 1] + 'a' + word[i + 1:len(word)]) ... i += 1 ... i2 += 1 ... i4 = 0 ... i5 = 0 ... for word in new: ... wordPool.append(word) ... print 'i3: ', i3 ... for word in wordPool: ... while len(i3) > i4: ... new.append(word[0:i3[i4]] + 'a' + word[i3[i4]:len(word)]) ... i4 += 1 ... while len(i3) > i5: ... new.append(word[0:i3[i5] + 1] + 'a' + word[i3[i5] + 1:len(word)]) ... i5 += 1 ... i4 = 0 ... for word in new: ... i = 2 ... while i < len(word): ... if word[i - 2] == word[i - 1]: ... if word[i - 1] == word[i]: ... new.remove(word) ... i += 1 ... for word in new: ... if word[len(word) - 3:len(word)] == 'aaa': ... new.remove(word) ... for word in new: ... wordPool.append(word) ... i = 0 ... i2 = 0 ... for word in wordPool: ... for test in wordPool: ... if i != i2: ... if test == word: ... wordPool.remove(test) ... i2 += 1 ... i += 1 ... for word in wordPool: ... print 'second: ',word -- http://mail.python.org/mailman/listinfo/python-list
Re: Meta Class
You're right I totally misunderstood it. And your idea is obvious and simple enough :) On Feb 1, 2008 6:33 PM, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > En Fri, 01 Feb 2008 15:46:05 -0200, Trevor Johnson > <[EMAIL PROTECTED]> escribió: > > > I think I have a good candidate for a meta class here. Never done this > > before and would like someone to help. In the code that follows, there > is > > one variable that needs to be changed: the letter 'a' as inserted in > > construction of the variable 'word'. In other applications, I will need > > to > > change that to two variables, but they are independent within this code. > > How > > do I go about abstracting these variables to make a meta class? > > I think you totally misunderstood the metaclass concept. A class is an > instance of its metaclass, that is, a metaclass is the "thing" used to > create a new class. You don't even use (custom) classes in your example. > > If you want to make a more generic function, that is, something that works > for other letters instead of just 'a', you want a function parameter: > > > >>> def testing(searched_letter): > > ... for word in wordPool: > ... > > and replace all occurences of 'a' with searched_letter, and 'aaa' with > searched_letter*3 > > Usage: testing('a'), it should give the same results as before. Try > testing('e') etc. > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list