I don’t know exactly what you want to do, but there are some ideas:
Initially you can remove class nesting and instantiate a property:
class CrudTDados:
def __init__(self, parent):
self._parent = parent
def select(self):
print("select")
def delete(self):
print("delete")
class CrudRegionalSPC():
def __init__(self):
self.CrudTabelaDados = CrudTDados(self)
crud_regionalspc = CrudRegionalSPC()
crud_regionalspc.CrudTabelaDados.select()
You can get the same result using decorators. Decorators can be functions or classes that add functionality to existing functions.
class CrudTDados:
def __init__(self, parent):
self._parent = parent
def select(self):
print("select")
def delete(self):
print("delete")
class CrudRegionalSPC():
@CrudTDados
def CrudTabelaDados(self): pass
crud_regionalspc = CrudRegionalSPC()
crud_regionalspc.CrudTabelaDados.select()
You can also create an abstract class and subclass it. This way you can exchange these subclasses:
from abc import ABC, abstractmethod
class ICrud(ABC):
@abstractmethod
def select(self): pass
@abstractmethod
def delete(self): pass
class CrudMudo(ICrud):
def select(self): pass
def delete(self): pass
class CrudTDados(ICrud):
def select(self):
print("select Dados")
def delete(self):
print("delete Dados")
class CrudTClientes(ICrud):
def select(self):
print("select Clientes")
def delete(self):
print("delete Clientes")
class CrudRegionalSPC():
def __init__(self, crud=CrudMudo()):
self.CrudTabelaDados = crud;
def MudarCrud(self, crud):
self.CrudTabelaDados = crud
crud_regionalspc = CrudRegionalSPC()
crud_regionalspc.CrudTabelaDados.select()
crud_regionalspc.MudarCrud(CrudTClientes())
crud_regionalspc.CrudTabelaDados.select()
crud_regionalspc.MudarCrud(CrudTDados())
crud_regionalspc.CrudTabelaDados.select()
Could you elaborate on why this syntax needs to be? XY problem, and Flat is Better than nested it seems to me that your scenario is wanting to have guarantees that python typing could solve. And, it is not usual this mixture of snake_case and camelCase in the same instruction.
– Enrique S. Filiage
@As I said in the question, it did not necessarily have to be one class within another, it could be another element. My need was to facilitate the process, because before I had a class with a tbl_a_select(), tbl_a_update(), tbl_a_delete() ..., tbl_b_select(), tbl_b_update(), tbl_b_delete() ... So I thought because there is no such thing as name_datatable.name_da_table.select() Got it? here is better to understand, with the solution of Augusto Vasques
– Wellington Fonseca