R
R
Refiru2020-08-19 15:40:44
Python
Refiru, 2020-08-19 15:40:44

Does it matter which approach to use, from a Python point of view?

Hello. There is code that does the same thing. Is there a difference from a python point of view? Or you can use it however you like.

lib = ctypes.CDLL(path)

def WrapFunction(lib, funcname, restype, argtypes):
    func = lib.__getattr__(funcname)
    func.restype = restype
    func.argtypes = argtypes
    return func

# первый вариант

CTestFunc = WrapFunction(lib,'TestFunc',None,[c_int,c_int,POINTER(ctypes.c_char)])
def TestFunc(int_width, int_height, str_title):
    return CTestFunc(int_width, int_height, str_title.encode('utf-8'))

# или такой вариант (мне он больше нравится).


def TestFunc(int_width, int_height, str_title):
    TestFunc = WrapFunction(lib,'TestFunc',None,[c_int,c_int,POINTER(ctypes.c_char)])
    return TestFunc(int_width, int_height, str_title.encode('utf-8'))

# Знаю про PEP-8, но мне для обратной совместимости так удобнее. Называть функции.
# Подскажите пожалуйста есть ли разница? Для производительности, около 200 таких враперов.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Ternick, 2020-08-19
@Refiru

1) Python does not really care what code to execute and it also doesn’t care about optimization, it’s up to you to think about it :)
2) You can measure the speed of code execution using the timeit library.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question