-3

I'm working on this OOP program in Python

class SimpleString():    
    popSize = 1000 #Should be even number
    displaySize = 5
    alphatbet = "abcdefghijklmnopqrstuvwxyz "
    def __init__(self):
        pop = numpy.empty(self.popSize, object)
        target = self.getTarget()
        targetSize = len(target)

    def genNewPop(self):
        for i in xrange(self.popSize):
            pop[i] = Item(self.genNewString())

    def genNewString(self):
        s = ""
        for i in xrange(targetSize):
            s += chr(random.randint(len(alphabet)))
        return s

def main():
    ss = SimpleString()
    ss.genNewPop()
main()

In the genNewString(self) method it keeps telling me the instance variable targetSize is not defined! I tried putting self.targetSize in its place but then I get an error SimpleString instance has no attribute 'targetSize' even though it is assigned as an instance variable.

0

1 Answer 1

5

All members in Python require the explicit self.:

def __init__(self):
    self.pop = numpy.empty(self.popSize, object)
    self.target = self.getTarget()
    self.targetSize = len(self.target)

def genNewString(self):
    s = ""
    for i in xrange(self.targetSize):
        s += chr(random.randint(len(self.alphabet)))
    return s
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.