Pyo3: How to expose enums and constants to python module?

Created on 6 Mar 2018  路  6Comments  路  Source: PyO3/pyo3

If I'm using pyo3 to create a module called Test and I have some rust code like this

pub enum Event{
    A(u32),
    B(u32),
    ...
}

const X: u32 = 0;

How can I expose these to Python, e.g. as Test.Event.A and Test.X?

Most helpful comment

@konstin Can you create a small, complete example of how to make bindings that pass python enum.Enum to rust and back again? I keep getting errors each time I try to do it, and I'm the point where I'm passing u8 back and forth just because it's easier, but it really breaks the mental model to do so.

All 6 comments

i dont know :)

maybe class properties? you can create class and expose some properties or static methods

technically, i think it is possible to bridge rust enums to a python enums, at least some subset, but that will require time

Hi @fafhrd91, because my project needs to export Rust enum to Python enum, I'm wondering if it makes sense that turn Rust enum to PyTuple that already implemented in PyO3, that according to Python document says the semantics of Python enum API resemble namedtuple?

Or I just make it as class...which seems also work as you commented.

@snowmantw I don鈥檛 maintain this library anymore. @konstin is maintainer

@konstin Can you create a small, complete example of how to make bindings that pass python enum.Enum to rust and back again? I keep getting errors each time I try to do it, and I'm the point where I'm passing u8 back and forth just because it's easier, but it really breaks the mental model to do so.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

konstin picture konstin  路  5Comments

konstin picture konstin  路  5Comments

fubuloubu picture fubuloubu  路  7Comments

tcztzy picture tcztzy  路  4Comments

thedrow picture thedrow  路  5Comments