Skip to content

String Representation of an Enum #575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jaredbroad opened this issue Nov 19, 2017 · 7 comments
Closed

String Representation of an Enum #575

jaredbroad opened this issue Nov 19, 2017 · 7 comments

Comments

@jaredbroad
Copy link

Environment

  • Pythonnet version: All
  • Python version: 2.7
  • Operating System: Mono

Details

  • Print string name of an enum from python.
   // in C#
   enum NamePrinting {
       Test,
       Name
   }

   // in python:
   self.A = "A" + str(NamePrinting.Test)
   print A
   // A1 not "ATest" like in C#.

Is it possible to get the text string name instead of the integer?

@den-run-ai
Copy link
Contributor

@jaredbroad the problem here is that C# enum is converted to its value (int) before you can do anything with this enum in Python. Here are possible fixes for similar issue, that I suggested before:

#174 (comment)

@aparamon
Copy link

Current str() representation of .NET enums is sub-optimal per se (strong .NET typing information is lost) and also is an incompatibility with IronPython.

@denfromufa suggested several approaches to support better enum str() representation, including

  • special case enums in overloading method selection to allow inter-operability with integer types.

What about using IntEnum? According to the docs, special casing enums may not be at all required as IntEnums are already ints.

@den-run-ai
Copy link
Contributor

@aparamon this would work only in Python 3 which is ok. This contribution is welcome!

@scenapp
Copy link

scenapp commented Nov 11, 2020

Enums are enums in .NET - this is a very weird decision to replace with integers. Are we back to the 'error code days' ?

@filmor
Copy link
Member

filmor commented Nov 11, 2020

Contributions are welcome. Python.NET is a very old library, dating back to Python 2.2 (I think), IntEnum etc. didn't exist back then.

@MarioQuillas
Copy link

I tried to replicate this issue because I'd like to contribute but this seems to work for .net versions greater than 4.6

@filmor
Copy link
Member

filmor commented Apr 26, 2021

This was indeed implemented by @lostmsu in #1392 (so it works with builds from the current master, independent of the .NET version).

@filmor filmor closed this as completed Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants