Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How I can fill class members?
#1
If I set the class fields in the __init__ method, they will be set in every time when object is created, which is incorrect. At that not all classes have method __init__ (for example, singleton).

class _AbstractInserter(metaclass=ABCMeta):
    TABLE_NAME: str
    MAPPING = _AbstractInserter._create_mapping()
    CODE_SQL = _AbstractInserter._create_code_sql()
       
    @classmethod
    @abstractmethod
    def _create_mapping(cls) -> dict:pass
    
    @classmethod
    def _create_code_sql(cls) -> str:
        code_sql = ''
        for key, value in cls.MAPPING.items():
            code_sql += key + '=' + value
        return code_sql

class MyInserter(_AbstractInserter):
    TABLE_NAME = 'MyTable'

    @classmethod
    def _create_mapping(cls) -> dict:
        mapping = {}
        mapping[cls.TABLE_NAME] = 'id'
        return mapping
Output:
MAPPING = _AbstractInserter._create_mapping() NameError: name '_AbstractInserter' is not defined
I get this error even if I use this code:
from __future__ import annotations
How can I do this?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Get variable from class inside another class hcne 3 97 Mar-30-2019, 03:02 PM
Last Post: ichabod801
  Learn to use a JSON file to fill out Template Automatically Srotkowitz 1 100 Mar-06-2019, 08:15 PM
Last Post: Larz60+
  Why Can't I call base class function for my derived class object? AmirAB 2 142 Feb-13-2019, 03:04 PM
Last Post: snippsat
  How to fill empty values with mean SriRajesh 4 265 Jan-20-2019, 01:10 PM
Last Post: SriRajesh
  Calling a class inside other class dan789 9 350 Jan-06-2019, 02:00 PM
Last Post: dan789
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 344 Dec-03-2018, 11:16 PM
Last Post: Windspar
  help for Kaggle Titanic Set fill the missing Age by median age of Pclass and Sex Parthasarathi009 2 382 Nov-21-2018, 06:50 PM
Last Post: Parthasarathi009
  How to fill a web form integrating with excel praveen_Akepati 0 293 Oct-27-2018, 06:23 AM
Last Post: praveen_Akepati
  How to check if class instance exists in a list of class instance objects? sonicblind 23 2,301 May-27-2018, 05:44 AM
Last Post: buran
  Empty attribute class dictionary after saving it in a class object dictionary 3dimensions 6 938 May-20-2018, 01:57 PM
Last Post: 3dimensions

Forum Jump:


Users browsing this thread: 1 Guest(s)