Python Forum
Is a way to create object instance from dict as argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is a way to create object instance from dict as argument
#1
We have class:

class DbConnection:
    def __init__(self, host: str, port: int, db_name: str):
        self.__host = host
        self.__port = port
        self.__db_name = db_name

    def get_host(self) -> str:
        return self.__host

    def get_port(self) -> int:
        return self.__port

    def get_db_name(self) -> str:
        return self.__db_name


class DBCommunicator:
    def __init__(self, db_connection: DbConnection):
        self.__db_connection = db_connection

    def get_connection(self) -> DbConnection:
        return self.__db_connection
Is a way to create instance like:

arguments = [
   db_connection: DbConnection()
]

object_instance = DBCommunicator(arguments)
In __init__ method I need bound arguments with typehints as in my class code. But I want to have possibility to create instance with different arguments. Like autobound.

Can you help me?
Reply
#2
I've found solution:

arguments_list = []
instance = SomeClass(*arguments_list)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020