Is abstract class field in python possible?

Attribute trouble when instantiating a class in Python 2.7

  • 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.

  • Answer:

    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.

COBRA! at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

Oh Jesus Christ I'm an idiot.

COBRA!

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.