0

I'm looking for functionality like this in C:

// driver.h
void some_func(struct _struct* prod);

// driver_thisproduct.c
void some_func(struct _struct* prod) {
// do some stuff
}

// driver_thatproduct.c
void some_func(struct _struct* prod) {
//do some stuff
}

//Makefile(sorry I don't know much about Make)
if model == 'thisproduct'
    obj += driver_thisproduct.o
elif model == 'thatproduct'
    obj += driver_thatproduct.o

by this, I can link different function by linking different object file.

I thought I could do like this in python:


#this_product.py
def some_func(self):
    # do some stuff


# that_product.py
def some_func(self):
    # do some stuff


# driver.py
class Driver:
    def __init__(self):
        product = input()
        if product == 'thisproduct':
             importlib.import_module('thisproduct')
        elif product == 'thatproduct':
             importlib.import_module('thatproduct')

by this, I thought I could do dynamic linking, but I couldn't do it. I've read How can I separate the functions of a class into multiple files?, but I couldn't get what I expected. What did I do wrong? is using importlib.import_module and import ... different?

me in let
  • 13
  • 3
  • 3
    What *are* you expecting? What is the actual problem you need to solve? Why do you need to do "dynamic linking"? – Some programmer dude Jul 04 '22 at 09:51
  • @Someprogrammerdude I'm working on a NAPALM project. napalm uses import_module to import driver implementation. company I'm working for has several devices, and wants to handle all of their devices by using one driver. each device has different CLI output, so I wanted to get model name on parent-driver, so that it could import child-driver. the hirearchy should be look like this: grandparent-driver(napalm.base)->parent-driver(company driver)->child-driver(imported by model) – me in let Jul 04 '22 at 11:09

1 Answers1

0

You could just rename the module selected by user input:

#this_product.py
def some_func(self):
    # do some stuff


# that_product.py
def some_func(self):
    # do some stuff

# driver.py
import that_product
import this_product

userSelectedProduct = None
product = input()

if product == 'thisproduct':
    userSelectedProduct = this_product
elif product == 'thatproduct':
    userSelectedProduct = that_product
else:
    # Raise error
    pass

userSelectedProduct.some_func()
...
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47