__slot__: what is it good for?

Discussion in 'Python' started by Alexander Eisenhuth, Jun 21, 2010.

  1. Hello out there,

    - what is the reason, that __slots__ are introduced in python?

    - I want to use slots to define a class where no attributes are added at
    runtime. Is that a good idea to use slots for that?


    Regards
    Alexander
     
    Alexander Eisenhuth, Jun 21, 2010
    #1
    1. Advertisements

  2. Alexander Eisenhuth

    James Mills Guest

    Here is the relevant documentation:'

    http://docs.python.org/reference/datamodel.html#slots
     
    James Mills, Jun 21, 2010
    #2
    1. Advertisements

  3. Alexander Eisenhuth

    Peter Otten Guest

    When you have "many" instances of a class with a fixed set of attributes
    __slots__ can save you some memory because it avoids the overhead for the
    instance __dict__. Note that "many" means millions rather than thousands.
    The best approach is to measure the memory footprint of your app with and
    without __slots__ in the class you see as a good candidate and only use it
    when you find a significant difference.
    No.

    Peter
     
    Peter Otten, Jun 21, 2010
    #3
  4. Errr.

    The Pythonic way to achieve that is to... simply NOT add attributes at
    runtime.

    I.e., choose to follow the rule you've decided on.

    --

    Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.10 (Darwin)

    iQEcBAEBAgAGBQJMH4QmAAoJEKcbwptVWx/lMbkH/R7yeT3uAWMcQvjgxjGY5PTP
    n7L0f3q9CdRKItIFjcfGJ9uUTQBcrZ8VKSg1pME21A3rDT+ElkA8iR01qEQ6PsOK
    1Z4wL3hbsEQ5SSYBNZYPHq1rPmWi/lJ0CqNPn48IY7yRgsztQnv50PAJ2Q04ietj
    yNeAhIQfbrF15o7ZQ90DHu+Zm7+ZNtQa+MnBwLYwoLi20gJQk3dt5hAFXyLqnPG3
    E/pa217QEuXLBb/wRDteKZcAli2SUGYsnAdU81cuPwcQRPl+xY7p/yNX1EWs620x
    JVesXnSoLXq2zgD+7R0HxHcagZJYVhAtywO8zOrlfZxNLDjYNbilza9DFfT5tqY=
    =pPJW
    -----END PGP SIGNATURE-----
     
    Stephen Hansen, Jun 21, 2010
    #4
  5. Alexander Eisenhuth

    Terry Reedy Guest

    Or add a __setattr__ method, but the above is easier.
     
    Terry Reedy, Jun 21, 2010
    #5
  6. I'd sort of argue that adding a __setattr__ for that purpose is
    un-Pythonic (as opposed to using __setattr__ for some functional purpose
    and that restriction being a side-effect). Its possible to do it, sure;
    but its also possible to use __slots__ to do so as well. But its bad
    form in both cases.

    But, that argument is largely Nit meeting Pick and having a fight about
    which kettle is blacker. :)

    --

    Stephen Hansen
    ... Also: Ixokai
    ... Mail: me+list/python (AT) ixokai (DOT) io
    ... Blog: http://meh.ixokai.io/



    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.10 (Darwin)

    iQEcBAEBAgAGBQJMH7WUAAoJEKcbwptVWx/ldKoH/AsDuR7je1zUi7KCpQb/dWAW
    sb8vHU6cqu95hVE848wGnl2ajzjEWyZPVu/J1bbur37Zrd/DHOYNAeFygf8oW6/4
    RrFBBoQUYjaEneogwM7oUSdK8iNaWawtzsFxPEVNKbnG0qHHodykv8WvSIZLkrly
    nIZZHCn0tIWe1drsuRfVdg+f6l66Ak57yuDpspJRiTkVVwFcphKzmsivo7rzkBn1
    5W3y0O1S5zlJ9wk2p8fUhd5aMC03kAcWICLOpCHx0OOXt1APD48f3+98mLP9XYBD
    QGOx0+T7ynz7EbiY1xirPgVKITRQBXVnpk9cxXdgDNlbmF6skUbmBP9+whVqAdI=
    =+VzQ
    -----END PGP SIGNATURE-----
     
    Stephen Hansen, Jun 21, 2010
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.