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
Other answers
Oh Jesus Christ I'm an idiot.
COBRA!
Related Q & A:
- How to construct a class diagram?Best solution by sourcemaking.com
- Is it correct to extend a bean class and write a class which holds logic to populate the bean we extend?Best solution by stackoverflow.com
- How to verify a JWT using python PyJWT with public key?Best solution by Stack Overflow
- how to call a function in Python in another function?Best solution by Yahoo! Answers
- What happens when you prestige in modern warfare 2?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.