ruby %w equivalent

Nick Craig-Wood nick at craig-wood.com
Wed Sep 27 11:30:03 EDT 2006


Duncan Booth <duncan.booth at invalid.invalid> wrote:
>  Nick Craig-Wood <nick at craig-wood.com> wrote:
> 
> > In python when making __slots__ or module.__all__ you end up typing
> > lists of objects or methods and they turn out like this which is quite
> > a lot of extra typing
> > 
> >   __slots__ = ["method1", "method2", "method3", "method4", "method5"]
> 
>  For __all__ you can use a decorator to avoid retyping the function name at 
>  all. e.g.
> 
>  def public(f):
>      all = f.func_globals.setdefault('__all__', [])
>      all.append(f.__name__)
>      return f
> 
>  @public
>  def foo(): pass

Nice one!

>  I don't use __slots__ much at all, and if you'd said "attribute1" etc. I'd 
>  have understood, but I'm really curious why would you be listing any 
>  methods in __slots__?

Those should of course have been attributes - I noticed immediately
after posting ;-)

Aside: __slots__ is only really useful when you've created so many
objects you are running out of memory and you need to optimise memory
usage a bit.  We got our app down to 1/3 of the memory usage by
putting in three __slots__

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list