Quesion about the proper use of __slots__

Zefria zefria at gmail.com
Mon Feb 20 04:14:20 EST 2006


>>> 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 "<stdin>", 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.




More information about the Python-list mailing list