Composition of objects in Python

Asked

Viewed 291 times

4

In this case, I’m trying to understand how the connection between two classes and their objects.

Since, when creating an object, a space in memory would be reserved for it, this space includes the object by which it is composed?

Example:

class Cliente:
  def __init__(self, nome, idade):
    self.nome = nome
    self.idade = idade
    self.enderecos = []

  def insere_endereco(self, cidade, estado):
     self.enderecos.append(Endereco(cidade, estado))

  def lista_endereco(self):
     for endereco in self.enderecos:
        print(endereco.cidade, endereco.estado)



class Endereco:
  def __init__(self, cidade, estado):
    self.cidade = cidade
    self.estado = estado

In this example, the class object Endereco is being stored in the memory space reserved for your respective customer? That is why, when deleting a customer, the respective address is also deleted?

1 answer

3


If you have an inheritance there are no two objects, there is only one that is created based on a model that is defined in two parts. You can see more about What is the difference between a class and an object?.

But in this case you are only using composition, so you have one object that contains another. That "contains" is what needs to be explained. Doesn’t mean one object is inside another.

All objects you create with a class exist by themselves and are always allocated independently. The object exists in two parts: a reference that points to the object, and the object itself. It is a type by reference. It would be a pointer, but in Python it’s even a little more complicated than that.

A list is also an object by reference. So self.endereços , roughly speaking, it only has the pointer to the list itself. The list is allocated to another memory location, it is not inside Cliente. There inside Cliente has only the pointer.

Inside this list object you will have pointers to objects. In this case we see that the code places objects of type Endereco. This object that stores the address is in another memory position, it is not inside the list that has only one pointer to the real object.

There’s a connection from Cliente with the list called enderecos that has a connection to an object Endereco. Connection is different than being all together inside each other.

When an object is deleted, Python’s memory management mechanism, which is primarily a reference count, will do a job to see if it should erase the objects attached to it. If no one else refers to that object it is deleted. But if somewhere else you have a reference to a Endereco for example, this object will not be erased, it needs to survive not to make unviable this other location.

You can see more details on How Python manages memory during assignment of different types? and What is the difference between Association, Aggregation and Composition in OOP?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.