Python: Run class or element inside a class

Asked

Viewed 55 times

0

there is some way to execute a def that is within a class or some other element that I do not know about without the need to include the () of the class, only with the name? See the example below of how I wish it to work, if possible.

It need not necessarily be one class within another, it may be another element, but it is possible to execute in this way crud_regionalspc.CrudTabelaDados.select()

class CrudRegionalSPC:
    class CrudTabelaDados:
        def select(self):
            print("select")

        def delete(self):
            print("delete")


crud_regionalspc = CrudRegionalSPC()

# como funciona
crud_regionalspc.CrudTabelaDados().select()

# como eu gostaria que funcionasse
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.

  • @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

2 answers

3

I have to say that it’s a very confusing and very biased structure to be a XY problem.

But by way of knowledge, by defining the method as static it will basically act as a normal function within the class, so there is no need for the self as parameter, since it will belong to the class and not to the instance.

class CrudRegionalSPC:
    class CrudTabelaDados:
        @staticmethod
        def select():
            print("select")

        @staticmethod
        def delete():
            print("delete")


crud_regionalspc = CrudRegionalSPC()

# como eu gostaria que funcionasse
crud_regionalspc.CrudTabelaDados.select()  # select

See working on Ideone

Even you wouldn’t even need the instance to call the desired function:

CrudRegionalSPC.CrudTabelaDados.select()

Because then the classes would be acting basically as namespaces.

  • It worked perfectly, as I wanted, but my need still needs the self. But it has already given me a parameter of what I can do. The problem issue is that I always have to create a tabela_select function, tabela_update, tabela_delete and so on for others. I was thinking of setting up something that would be simpler example Datatable.Tabela.select() Thanks so much for the help.

  • 1

    So it really is an XY problem and this is not the ideal way to solve your problem.

1


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()
  • 1

    Your first example fell like a glove for what I need, follows the ideone to understand my idea.

Browser other questions tagged

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