Quesion about the proper use of __slots__

bruno at modulix onurb at xiludom.gro
Mon Feb 20 03:47:56 EST 2006


Zefria wrote:
>>>>class Fighter:

Old-style classes are deprecated, use new-style class wherever possible:

class Fighter(object):
  ....

> ...     '''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]
> ...
> 
(snip)



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

"premature optimization is the root of all evil".

> 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?
> 

I think it's:

class Fighter(object):
  __slots__ = ['fuel', 'life', 'armor', 'weapon', 'bulk',]
  def __init__(self, ...):
    # code here



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list