[Python] Nanobind: How to use class from another module inside module?

Discussão em 'Python' iniciado por Stack, Maio 24, 2025.

  1. Stack

    Stack Membro Participativo

    I currently have several NB_MODULEs, and those have some nb:classes which are identical. I'm looking to move those to a common place so that each NB_MODULE can use them from the same location, eliminating some duplicate code.

    I have created another module called common_definitions and added one class there like this:

    NB_MODULE(common_definitions_ext, m)
    {

    nb::class_<example_t>(m, "example_t")
    .def(nb::init<int64_t, int32_t>())
    .def_rw("utc", &example_t::u_utc_time)
    .def_rw("time_zone", &example_t::s32_time_zone);

    }


    Now, I can create such an object from a test case like this:

    date = common_nb_definitions.example_t(1701340147, 180)


    However, when I try to assign that to another module:

    input = another_module.another_module_input()
    input.s_date = date


    It fails like this:

    input.s_date = date
    ^^^^^^^^^^^^
    TypeError: (): incompatible function arguments. The following argument types are supported:
    1. (self, arg: s_example_t, /) -> None


    Any suggestions would be much appreciated!

    Continue reading...

Compartilhe esta Página