I run into a problem, I'm looking for the python equivalent of a C++ language hierarchical record, The records are as follows in c++:
struct Fecha
{
int day;
int month;
int year;
};
struct Informacion
{
int codigo;
float precio;
Fecha ingreso,nacimiento; //aquí esta el vinculo con "struct fecha"
char descripcion[40];
};
In turn, it is a registry array, in the main() it is declared:
Informacion arreglo[40];
The problem, I'm not finding its equivalent in python, I've searched but I can't find the solution, I'm not sure how to plant it in python
There are several alternatives to implement C structures in Python.
The easiest is to just use a dictionary:
If you want to be a bit stricter, you can implement a class:
That is the minimum required. The only additional advantage is that it is now clear that it
f
is an object typeFecha
, not just a dictionary.Objects in Python are basically plugged-in dictionaries. They do not require getters or setters. You can add new keys and change values directly. There is no such thing as private data/methods in Python, although there are naming conventions to express that idea.
The advantage of classes in Python is that you can implement magic methods to facilitate the use of objects: printing, serialization, comparison operations, etc (just like the overload in C++).
Example. The method
__str__
is called when Python needs to print an object:produces
In Python arrays are not declared. As they are not declared, they can grow without restrictions, both in number of elements and in dimensions. You just create them and then add elements to them.
Example:
In Python, the attributes of a class can be instantiated in a similar way to the definition of variables in a data structure (struct in C), by means of
__slots__
. I think that's the most simulable way, I say this since it's done through a class (which is not the same as a data structure):In C:
O well
In Python:
I don't know if there is a way in Python to initialize the attributes of an object through a list (Like in C with an array)
Going deeper, how does it work
__slots__
?As I already said,
__slots__
it allows you to instantiate the attributes of a class in a similar way to the structs in C. This by means of a list (the definition of__slots__
), the strings that the list must contain will be interpreted as the names of the variables that represent attributes.Sentence
It is equivalent to
What differences are there between this?
The main difference is that it
__init__
is a method that is called when an object is initialized based on said class,__init__
it depends on parameters when initializing the object to define attributes. This is not necessary in all cases, since attributes can sometimes be defined internally without depending on variables or values external to the class.On the other hand,
__slots__
it is a constant inside the class that instantiates all the attributes (via the list that is used). For this reason, its values must be defined externally to the class:Which should I or is more convenient to use?
__slots__
does not define values in memory space instantly, only instantiates them . It leaves open the possibility of defining them.Answering the question, it is better to use the method
__init__()
to initialize the object with necessary attributes from its initialization. On the other hand,__slots__
when certain attributes are not needed from the initialization of the object, or it is not known if it has to be initialized.Links that may help you:
I think this question (which is already old) can help you.
I hope I have served you, greetings!