Consume instance of existing class same as the parent by the child class

I can’t directly overwrite all of the parent attributes from a child with an already instantiated class that is of the same type as the parent the child inherits from.

What I’m trying to achieve is in a child class directly overwrite all its parent attributes by an already instantiated class that is the same as the parent.

The answer in this question describes good what I want: Create child class instance from parent class instance

If you mean (as @Barmar suggested) that you want a way to copy all the attributes of an instance of Foo over to an instance of Bar, that is another story. In that case, you still need to be careful to define, what exactly you mean by “all attributes”.

With “all attributes” I mean just copy over mine defined ones. Like the same way as pydantic see and grab them for its typing context. And in an automatic way, because most classes don’t have just 10-20 attrs, but 50-200. And set each one by one in init by hand to copy its value over would be overkill, as they are already once written down in the respective class.

The design problem is, the policy must do the first request to the database to get the case_id of the records ID it refers to, so with that gathered knowledge it knows how to load the details of that policy from the database.
That’s because I load a python file ({case_id}.py) where I store the specific infos on what tablename to lookup and what fields there are. So it is clean seperated.

main.py

from Models.Policy import Policy
from Models.PolicyDetails import PolicyDetails
policy = Policy()
policy.findById(policy_id)
details = PolicyDetails()
details.loadFrom( policy )

Policy.py

from pydantic import BaseModel
from .ActiveRecord import ActiveRecord

class Policy(BaseModel, ActiveRecord):
    id: int = None
    case_id: str = None
    v_number: str = None
    g_id: str = None

    def __init__(self):
        BaseModel.__init__(self)
        ActiveRecord.__init__(Policy)
    
    def tableName(self):
        return 'gn_policy'
    
    def idName(self):
        return 'identifier'
    
    def getTableColumnMap(self):
        return {
            "identifier": "id",
            "case_description": "case_id",
            "pol_doc_number": "v_number",
            "grow_val_identifier": "g_id"
        }

PolicyDetails.py
In this class I load dynamically {case_id}.py files and instanciate them and want to consume the child.

import importlib
from pydantic import BaseModel
from .Policy import Policy

class PolicyDetails(BaseModel):

    details: object = None
    
    def __init__(self):
        pass
    
    def loadFrom(self, policy: Policy):
        case_id = policy.case_id
        case_class = self.load_class(case_id)
        case_instance = case_class(policy)
        
    def load_class(self, id: str):
        case_module = importlib.import_module("."+id, "Models")
        case_class = getattr(case_module, id)
        return case_class

Two {case_id}.py file looks like this:
GERM.py

class GERM(Policy):
    def tableName():
        return 'gn_GERM'
    
    address: str = None
    def getTableColumnMap(self):
        return {
            "adr": "address"
        }

IMP.py

class IMP(Policy):
    def tableName():
        return 'gn_IMP'
    
    firstname: str = None
    lastname: str = None
    def getTableColumnMap(self):
        return {
            "FI": "firstname",
            "LA": "lastname"
        }

The main goal is to populate/hydrate the policy instance with the delayed gathered details of it.

Is inheritance intended for solving this or moreover done by composition?

Thanks in advance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Consume instance of existing class same as the parent by the child class

I can’t directly overwrite all of the parent attributes from a child with an already instantiated class that is of the same type as the parent the child inherits from.

What I’m trying to achieve is in a child class directly overwrite all its parent attributes by an already instantiated class that is the same as the parent.

The answer in this question describes good what I want: Create child class instance from parent class instance

If you mean (as @Barmar suggested) that you want a way to copy all the attributes of an instance of Foo over to an instance of Bar, that is another story. In that case, you still need to be careful to define, what exactly you mean by “all attributes”.

With “all attributes” I mean just copy over mine defined ones. Like the same way as pydantic see and grab them for its typing context. And in an automatic way, because most classes don’t have just 10-20 attrs, but 50-200. And set each one by one in init by hand to copy its value over would be overkill, as they are already once written down in the respective class.

The design problem is, the policy must do the first request to the database to get the case_id of the records ID it refers to, so with that gathered knowledge it knows how to load the details of that policy from the database.
That’s because I load a python file ({case_id}.py) where I store the specific infos on what tablename to lookup and what fields there are. So it is clean seperated.

main.py

from Models.Policy import Policy
from Models.PolicyDetails import PolicyDetails
policy = Policy()
policy.findById(policy_id)
details = PolicyDetails()
details.loadFrom( policy )

Policy.py

from pydantic import BaseModel
from .ActiveRecord import ActiveRecord

class Policy(BaseModel, ActiveRecord):
    id: int = None
    case_id: str = None
    v_number: str = None
    g_id: str = None

    def __init__(self):
        BaseModel.__init__(self)
        ActiveRecord.__init__(Policy)
    
    def tableName(self):
        return 'gn_policy'
    
    def idName(self):
        return 'identifier'
    
    def getTableColumnMap(self):
        return {
            "identifier": "id",
            "case_description": "case_id",
            "pol_doc_number": "v_number",
            "grow_val_identifier": "g_id"
        }

PolicyDetails.py
In this class I load dynamically {case_id}.py files and instanciate them and want to consume the child.

import importlib
from pydantic import BaseModel
from .Policy import Policy

class PolicyDetails(BaseModel):

    details: object = None
    
    def __init__(self):
        pass
    
    def loadFrom(self, policy: Policy):
        case_id = policy.case_id
        case_class = self.load_class(case_id)
        case_instance = case_class(policy)
        
    def load_class(self, id: str):
        case_module = importlib.import_module("."+id, "Models")
        case_class = getattr(case_module, id)
        return case_class

Two {case_id}.py file looks like this:
GERM.py

class GERM(Policy):
    def tableName():
        return 'gn_GERM'
    
    address: str = None
    def getTableColumnMap(self):
        return {
            "adr": "address"
        }

IMP.py

class IMP(Policy):
    def tableName():
        return 'gn_IMP'
    
    firstname: str = None
    lastname: str = None
    def getTableColumnMap(self):
        return {
            "FI": "firstname",
            "LA": "lastname"
        }

The main goal is to populate/hydrate the policy instance with the delayed gathered details of it.

Is inheritance intended for solving this or moreover done by composition?

Thanks in advance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật