[Python] How to offer platform-specific implementations of a module?

Discussão em 'Python' iniciado por Stack, Novembro 23, 2024.

  1. Stack

    Stack Membro Participativo

    I need to make one function in a module platform-independent by offering several implementations, without changing any files that import it. The following works:

    do_it = getattr(__import__(__name__), "do_on_" + sys.platform)

    ...but breaks if the module is put into a package.

    An alternative would be an if/elif with hard-coded calls to the others in do_it().

    Anything better?

    Continue reading...

Compartilhe esta Página