Visitar URL original
Get Value Of An Enum · Issue #1585 · pythonnet/pythonnet · GitHub
Skip to content

Get Value Of An Enum #1585

Description

@saroad2

Environment

  • Pythonnet version: latest
  • Python version: 3.9.4
  • Operating System: Windows
  • .NET Runtime:

Details

  • Describe what you were trying to get done.

I would like to have the ability to extract the value of a .Net Enum via python. One can do that using MyEnum.value__, however it seems logical that you should do it using int(MyEnum).

import clr

clr.AddReference("System.Windows.Forms")

import System.Windows.Forms as WinForms

enum_value = WinForms.Keys.Control
print(f"{enum_value=}")
print(f"{enum_value.value__=}")

That prints:

enum_value=<System.Windows.Forms.Keys object at 0x000001C053E07580>
enum_value.value__=131072
  • If there was a crash, please include the traceback here.

No crash.

Activity

  1. filmor commented on Nov 23, 2021

    @filmor
    Member

    This is possible using codecs, though they are /really/ unergonomic so far when using them from Python:

     import Python.Runtime
     Python.Runtime.PyObjectConversions.RegisterEncoder(Python.Runtime.Codecs.EnumPyIntCodec.Instance)
     Python.Runtime.PyObjectConversions.RegisterDecoder(Python.Runtime.Codecs.EnumPyIntCodec.Instance)
    

    The decoder is buggy right now (fix in #1621).

    @lostmsu Do you have a proposal for a nice ergonomic Python API for this? Should we enable some encoders/decoders by default unless told otherwise?

  2. lostmsu commented on Nov 23, 2021

    @lostmsu
    Member

    I don't think enabling codecs by default is a good idea, unless they are 1:1 exact rountrippable mappings.

    As for the more ergonomic Python APIs, somebody more proficient with Python should propose those.

  3. lostmsu commented on Nov 23, 2021

    @lostmsu
    Member

    As for the issue at hand, .NET enums should implement __int__ or its C API alternative, nb_int for int(Keys.Control) to work, not a codec.

  4. filmor commented on Nov 23, 2021

    @filmor
    Member

    Isn't that something that we explicitly removed?

  5. lostmsu commented on Nov 23, 2021

    @lostmsu
    Member

    If I remember correctly, we removed the code, that converted any enum value to an integer implicitly when passed to Python. You could never previously get raw enum value in Python, it always just saw integers. So the corresponding type never had __int__ implementation.

  6. saroad2 commented on Nov 29, 2021

    @saroad2
    Author

    So if I understand you correctly, we all agree that adding a default __int__ method for all C# enums is a wise idea?

    I would have implemented this ability myself, but I have zero knowledge in C#. Let me know if I can help you somehow make this ability happen.

  7. added this to the 3.0.0 milestone on Nov 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions