Quesion about the proper use of __slots__
>>> class Fighter: ... '''Small one man craft that can only harm other fighters on their own.''' ... def __init__(self,statsTuple=(50,5,0,(2,4),1)): ... self.fuel = statsTuple[0] ... self.life = statsTuple[1] ... self.armor = statsTuple[2] ... self.weapon = statsTuple[3] ... self.bulk = statsTuple[4] ... __slots__ = [self.fuel,self.life,self.armor,self.weapon,self.bulk] ... >>> ral = Fighter() >>> ral.rocks = 2 >>> ral.rocks 2 >>> ral.life 5 I was reading the special methods, got to slots (http://docs.python.org/ref/slots.html) and decided that i should use this to save memory in the program because it'll have to support very large numbers of fighers at once. It says that once you define slots then you can't add any variables not listed in slots, but from that example section I just did, so am I doing something wrong or did I read it wrong? -- http://mail.python.org/mailman/listinfo/python-list
Re: Quesion about the proper use of __slots__
>>> class Fighter(object): ... '''Small one man craft that can only harm other fighters on their own.''' ... __slots__ = ["fuel","life","armor","weapon","bulk"] ... def __init__(self,statsTuple=(50,5,0,(2,4),1)): ... self.fuel = statsTuple[0] ... self.life = statsTuple[1] ... self.armor = statsTuple[2] ... self.weapon = statsTuple[3] ... self.bulk = statsTuple[4] ... >>> examp = Fighter() >>> examp.rock = 3 Traceback (most recent call last): File "", line 1, in ? AttributeError: 'Fighter' object has no attribute 'rock' >>> Thank you both, I had tried making it a class variable and didn't see anything. The trick must have been in making a class that inheriets from object. Also, I don't generally do any optimization at all yet (as a highschool student projects get trashed often enough no to bother over), but in this special case I'm expecting each "carrier" to have up to 150 fighters, and 3 to 5 carriers for each of the two teams, which comes out to be quite large. Additionally, this program's purpose was to experiment with as many of the special methods as possible, so I suppose I'm on track so far. -- http://mail.python.org/mailman/listinfo/python-list
Re: Quesion about the proper use of __slots__
Well, my computer tends to run at about 497 to 501 out of 504 MB of RAM used once I start up Gnome, XMMS, and a few Firefox tabs, and I'd prefer not to see things slipping into Swap space if I can avoid it, and the fighter data would be only part of a larger program. And as I said, learning how to use __slots__ is important enough to hazard a premature guess this time around. If I mess up, the worst that happens is that I have to write down a new variable name in two places instead of one now. -- http://mail.python.org/mailman/listinfo/python-list
Re: Quesion about the proper use of __slots__
I am aware of this; but with too much Java still running through my brain I've never bothered with the idea yet because that's simply not the way I learned and I've never had a chance to need new attributes for singular instances. "__slots__ is not a method, so it falls outside that purpose as stated. " -Alex Martelli It's listed within the special methods section of the Python Refrence, was what I ment. -- http://mail.python.org/mailman/listinfo/python-list
