Python Slots Inheritance

Posted onby admin

Python In Greek mythology, Python is the name of a a huge serpent and sometimes a dragon. Python had been killed by the god Apollo at Delphi. Python was created out of the slime and mud left after the great flood. He was appointed by Gaia (Mother Earth) to guard the oracle of Delphi, known as Pytho. Python Inheritance Inheritance allows us to define a class that inherits all the methods and properties from another class. Browse other questions tagged python inheritance slots or ask your own question. The Overflow Blog The Loop: Adding review guidance to the help center. Podcast 288: Tim Berners-Lee wants to put you in a pod. Featured on Meta A big thank you, Tim Post “Question closed” notifications experiment results and graduation. To have attributes named in slots to actually be stored in slots instead of a dict, a class must inherit from object. To prevent the creation of a dict, you must inherit from object and all classes in the inheritance must declare slots and none of them can have a 'dict' entry.

Latest version

Released:

Decorator to add __slots__ in dataclasses

Project description

Decorator for adding slots

Slots

Python3.7 provides dataclasses module for faster class creation (PEP 557).Unfortunately there's no support for __slots__. If you want to create more memory efficient instances, you need todo it by yourself or use dataslots.dataslots decorator.

Usage

Simple example

Inheritance

Python

As described in docs, in derived class __dict__ is created, because base class does not have __slots__.Slots are created from all defined properties (returned by dataclasses.fields() function).

Dynamic assignment of new variables

Weakref

Read-only class variables

With __slots__ it's possible to define read-only class variables. When using dataclasses you cannot provide typefor attribute or use typing.ClassVar to declare one.

Pickling frozen dataclass

Because of an issue 36424 you need custom __setstate__ method. In dataslots there isimplemented default version and it is used if decorated class has no __getstate__ and __setstate__ function declared.

More about __slots__

Release historyRelease notifications RSS feed

1.0.2

1.0.2rc2 pre-release

1.0.1

1.0.0

Python Slots Inheritance

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for dataslots, version 1.0.2
Filename, sizeFile typePython versionUpload dateHashes
Filename, size dataslots-1.0.2-py2.py3-none-any.whl (4.1 kB) File type Wheel Python version py2.py3 Upload dateHashes
Filename, size dataslots-1.0.2.tar.gz (7.5 kB) File type Source Python version None Upload dateHashes
Close

Hashes for dataslots-1.0.2-py2.py3-none-any.whl

Hashes for dataslots-1.0.2-py2.py3-none-any.whl
AlgorithmHash digest
SHA2564fe302ab59c86e01a4fafe516776a198cd8a42dc696dcc9d525e2ec8ee0fe773
MD5aa8075201eba64938a16361e741a901b
BLAKE2-256b2b22f9f4ea849a076effa673dd9b7e67bedb9358ad0875c30cd4ae0ad6298bc

Python Inheritance Multiple

PythonPythonClose

Hashes for dataslots-1.0.2.tar.gz

Python Slots Inheritance Money

Hashes for dataslots-1.0.2.tar.gz
AlgorithmHash digest
SHA2560dfc4d12aab104b00ddb88a585c0a2227bbb9bd19c785dc8068b43eb0d6009e1
MD5656b169564c8623fe9a97aa5f25df7fd
BLAKE2-256a81ca45405ae05d585b786e1819a3406310a097ffd7bf5f104e7c78e63cb86a8

Python Function Inheritance

P: n/a

Simon Brunning wrote:
On 14 Dec 2006 05:23:33 -0800, jm*******@no.spam.gmail.com
<jm*******@gmail.comwrote:
Hi all,
>From the google search, it seems its not possible to do the following.
>>class Test1(object):
... __slots__ = ['a']
...
>>class Test2(object):
... __slots__ = ['b']
...
>>class Test3(Test1,Test2):
... __slots__ = ['c']
...
Traceback (most recent call last):
File '<stdin>', line 1, in <module>
TypeError: Error when calling the metaclass bases
multiple bases have instance lay-out conflict
I just want to make sure that I am using only the attributes a,b and c
from the instances of Test3 . Is there any other hack that could be
done.

Difficulty with subclassing is the price you pay for abusing slots.
Slots are intended as a performance tweak only, to minimise the memory
footprint of classes of which you are going to have a great number of
instances.
In short - don't do that.
OK. But is there any other way to do what __slots__ does as a 'side
effect' i.e. forcing me to think about the list of attributes my class
is going to have upfront and raising error whenever I violate it. IMHO
this is a very good thing to have even if one does not care about
memory.
--
Suresh
>
--
Cheers,
Simon B
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/