Python Slots Vs Data Class

Posted on  by admin
Python Slots Vs Data Class
  1. There are a set of module-level functions for working with structured values, and there is also the Struct class (new in Python 2.5). Format specifiers are converted from their string format to a compiled representation, similar to the way regular expressions are.
  2. To list the attributes of an instance/object, we have two functions:-1. Vars– This function displays the attribute of an instance in the form of an dictionary. Dir– This function displays more attributes than vars function,as it is not limited to instance.

An inner class or nested class is a defined entirely within the body of another class. If an object is created using a class, the object inside the root class can be used. A class can have more than one inner classes, but in general inner classes are avoided.

Python Slots Vs Data Class

Related Course:
Python Programming Bootcamp: Go from zero to hero

Data classes are just regular classes that are geared towards storing state, more than contain a lot of logic. Every time you create a class that mostly consists of attributes you made a data class. The button click (signal) is connected to the action (slot). In this example, the method slotmethod will be called if the signal emits. This principle of connecting slots methods or function to a widget, applies to all widgets.

Inner class example


We create a class (Human) with one inner class (Head).
Python Slots Vs Data ClassAn instance is created that calls a method in the inner class:Python class data type

Python Class Data Member

Output:

In the program above we have the inner class Head() which has its own method. An inner class can have both methods and variables. In this example the constructor of the class Human (init) creates a new head object.

Multiple inner classes


You are by no means limited to the number of inner classes, for example this code will work too:

By using inner classes you can make your code even more object orientated. A single object can hold several sub objects. We can use them to add more structure to our programs.

Slot Machine In Python

Python Slots Vs Data Class

If you are new to Python programming, I highly recommend this book.