I have enum and use the variables like myEnum.SomeNameA
, myEnum.SomeNameB
, etc. When I return one of these variables from a function, can I print their names (such as myEnum.SomeNameA
) instead of the value they returned?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
There are two answers to this question: Yes and No.
How to do it:
It depends on an implementation of enums. For example:
It doesn't matter whether there are 100 names refers to an integer whose name you'd like to find if you know enums class object and all they're names (or at least a unique prefix) in that class.
For the example above as @batbrat suggested you can inspect '
Enum.__dict__
' usingnamestr()
:In this case you even don't have to know all enum names. If enums implemented differently then you might need to use a different hack. There will be some solution in most cases e.g., see @Jay's answer. But it doesn't matter because..
Is it worth it?
Some enum implementations may require ugly, complex and in the end unreliable hacks to implement
namestr()
.Enum class can return answer by itself (see @David's, @Ber's, @gahooa's answers).
As @Ber pointed out there is no builtin Enum and switch statement in Python (see PEP-0275, PEP-3103). It is worth investigating solutions without enums.
As far as I know, that will require some introspection. You can try using the inspect module.
There are a few simple things you may want to try before that:
Edit:
I just noticed that this is not what you want. In that case adding a dict and a function may work
All that said, there aren't standard enumerations in Python. It would help to know how you are creating them.
On second thoughts, you can maintain your variables as a dictionary in the enum, keyed by variable name and provide a method of the enumeration to find the right variable and print its name. This solution (keeping a dict) is bad because variable values aren't necessarily unique.
Edit:
The problem is not trivial, so you may want to use a tried and tested solution. IMHO, you would be better off avoiding the situation if you can.
On second thought:
Since Python does not provide native Enum types, you should not ask for one, but instead use other, more powerful construct to build your program. Otherwise, the next step will invariably be "Why does Python not have a
switch ...:
statement, and how do I best emulate it?"Since Enums are often used to define some kind of state, a much better approach is this: Create a base class that define all the abstract properties, attributes and methods belonging to a state. Then, for each state, derive a sub class that implements the specific behavior of this state. You can then pass around these classes (or maybe instances thereof) to handle the state and its behaviour.
If you use classes instead of instances (the Python way of a "singleton"), you can simply check for any given state (not that it should be necessary) by
if current_state is StateA:
(note theis
instead of==
) with no performance penalty over comparing integer values.And of course, you can define a
name
attribute and a__str__()
method to access and print the state's name.There is no such thing as a unique or original variable name http://www.amk.ca/quotations/python-quotes/page-8
To add to @Jay's answer, some concepts...
Python "variables" are simply references to values. Each value occupies a given memory location (see
id()
)From the above, you may notice that the value "1" is present at the memory location 10052552. It is referred to 569 times in this instance of the interpreter.
Now, see that because yet another name is bound to this value, the reference count went up by one.
Based on these facts, it is not realistic/possible to tell what single variable name is pointing to a value.
I think the best way to address your issue is to add a mapping and function to your enum reference back to a string name.
Please comment if you would like sample code.
You could store the canonical name as an attribute of the instance, and then assign it to a variable with the same name. This might work:
Regardless, it's not a particularly "Pythonic" thing to do.