Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JOSS] Slots #20

Closed
Sulstice opened this issue Mar 13, 2021 · 4 comments
Closed

[JOSS] Slots #20

Sulstice opened this issue Mar 13, 2021 · 4 comments
Labels

Comments

@Sulstice
Copy link

Forgive me but it's been awhile since I've seen the __slots__ argument. I remember some of the numpy folk arguing about it at the last PyData conference.

In src/kallisto/atom.py, Line 52.


    __slots__ = ["data", "molecule", "index"]

    def __init__(self, symbol="X", position=(0, 0, 0), charge=0, molecule=None):

        self.data = data = {}

Here's what I'm looking at, can you elaborate on your reason for choosing slots, and does it actually make access to your class attributes access that much faster?

@f3rmion f3rmion added the JOSS label Mar 13, 2021
@f3rmion
Copy link
Member

f3rmion commented Mar 13, 2021

Hi @Sulstice,

I used it to save RAM if someone wants to create objects with thousands of atoms, since default Python uses a dict to store an object's instance attribute. However, for small classes with known attributes, this could be quite a waste of memory.

You can try this yourself (I found a nice example here):

slots.py

class MyClass(object):
        __slots__ = ['name', 'identifier']
        def __init__(self, name, identifier):
                self.name = name
                self.identifier = identifier

num = 1024*256
x = [MyClass(1,1) for i in range(num)]

noslots.py

class MyClass(object):
        def __init__(self, name, identifier):
                self.name = name
                self.identifier = identifier

num = 1024*256
x = [MyClass(1,1) for i in range(num)]

We can check the memory usage with this excellent tool.

Screenshot 2021-03-13 at 18 04 11

The difference between both shows the memory aspect of __slots__ not sure about the speed though.
It seems to be faster, but this is not really a benchmark :)

JOSS

@f3rmion f3rmion added this to To do in Roadmap 2021 Mar 13, 2021
@RMeli
Copy link
Contributor

RMeli commented Mar 14, 2021

May I suggest to change the variable __slots__ into a tuple? This is nitpicking, but according to "Fluent Python" by Luciano Ramalho it better conveys the fact that the __slots__ definition can't change.

f3rmion added a commit that referenced this issue Mar 15, 2021
@f3rmion
Copy link
Member

f3rmion commented Mar 15, 2021

OK, I changed slots into a tuple to stress out its immutability.

@f3rmion f3rmion moved this from To do to In progress in Roadmap 2021 Mar 15, 2021
@f3rmion
Copy link
Member

f3rmion commented Mar 17, 2021

I think this can be closed since the main question (why using slots?) has been answered.

@f3rmion f3rmion closed this as completed Mar 17, 2021
@f3rmion f3rmion moved this from In progress to Done in Roadmap 2021 Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

3 participants