Join 3,375 readers in helping fund MetaFilter (Hide)


Attribute trouble when instantiating a class in Python 2.7
March 13, 2013 3:10 PM   Subscribe

I'm having maddening problems creating objects in Python. Details inside

This is the beginning of a class definition I'm using:

class popObj(object):
"""standalone class mainly holding list of binary trees and generation"""
def _init_(self):
#initializes two empty lists and a generation tracker
self.biTrees=[None]*POP
self.selectedTrees=[None]*POP
self.generation = 0

In the main module of the program, then, I call:

population=popObj()

I'm able to put objects into the list at population.biTrees[] with no problem. But when I try to do anything with population.selectedTrees or population.generation, I get this error:

AttributeError: 'popObj' object has no attribute 'selectedTrees'

What's going on? This is driving me nuts. I recognize that my class definition isn't very elegant, but I can figure out why some of the attributes don't exist.
posted by COBRA! to Computers & Internet (2 answers total) 1 user marked this as a favorite
 
You've misnamed "__init__", it looks like. It needs to have two underscores around it, not one. (underscore-underscore-init-underscore-underscore). So __init__ isn't running and it isn't creating those attributes. Possibly you're accidentally creating biTrees by accident somewhere else which is why that ones's working and the others aren't.
posted by BungaDunga at 3:15 PM on March 13, 2013


Oh Jesus Christ I'm an idiot.
posted by COBRA! at 3:18 PM on March 13, 2013 [1 favorite]


« Older I have impeccable dental hygie...   |  OK, so I've been mulling over ... Newer »
This thread is closed to new comments.