byteface / domonic

Create HTML with python 3 using a standard DOM API. Includes a python port of JavaScript for interoperability and tons of other cool features. A fast prototyping library.
https://domonic.readthedocs.io/
MIT License
131 stars 13 forks source link

slots #21

Open byteface opened 3 years ago

byteface commented 3 years ago

I read something about these improving performance but also heard they make your thing immutable. i then figured i could extend a slotted object? and the extended one wouldn't?. i put the placeholders in for slots on the html tag and commented them out for later testing and consideration but stil haven't read and understood enough about them tbh. So this will have to be something I come back to or take advice on.

Jordan-Cottle commented 3 years ago

If you are referring to __slots__, then you can probably safely start using it without worrying about making an object immutable.

It's useful for explicitly declaring instance attributes to avoid creating a __dict__ attribute that allows for dynamically assigning new attributes to objects. Since no __dict__ needs to be created, you save memory, and since no __dict__ lookups happen, attribute access also tends to be faster. All at the expense of just telling a class what attributes its instances will/should have up-front rather than waiting until the __init__ method is called to assign the attributes.

For more info you can read the data model docs at https://docs.python.org/3/reference/datamodel.html#object.__slots__

From a brief look at your tag class it looks like just __slots__ = ["args", "kwargs", "__content", "__attributes"] should work for you.

I opened a small PR that adds __slots__ to the tag class where the comment was.

byteface commented 3 years ago

This ticket was closed with a great example. But I'm leaving open so we can identify and apply slots to other classes.

Please do create a ticket for a given class if you want to make a __slots__ PR. So I can leave this one open until I feel we have enough of them covered.

byteface commented 3 years ago

a good pointer for slot useage exmples is the existing minidom.

byteface commented 2 years ago

thought i'd give an update on the status of slots. seems after version 8 we destroy the tag class in favour of just using Node but this the increased the methods on Node. since then pypy3 has an inssue i think with deepcopy in a slot or something and when i push if fails for pypy only.. so slots are commented off for now on Node until that gets resolved.