Enum class with ToString functionality
Bjoern Schliessmann
usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Mon Sep 10 07:39:34 EDT 2007
bg_ie at yahoo.com wrote:
> I have the following class -
>
> class TestOutcomes:
> PASSED = 0
> FAILED = 1
> ABORTED = 2
>
> plus the following code -
>
> testResult = TestOutcomes.PASSED
>
> testResultAsString
> if testResult == TestOutcomes.PASSED:
> testResultAsString = "Passed"
> elif testResult == TestOutcomes.FAILED :
> testResultAsString = "Failed"
> else:
> testResultAsString = "Aborted"
>
> But it would be much nicer if I had a function to covert to string
> as part of the TestOutcomes class. How would I implement this?
Why don't use the simple approach like this?
TEST_PASSED = "Passed"
TEST_FAILED = "Failed"
TEST_ABORTED = "Aborted"
In Python, no one forces you to put everything in classes.
If you are determined to use the class approach, use the __str__
method. It's called when you do str(instance).
Regards,
Björn
--
BOFH excuse #122:
because Bill Gates is a Jehovah's witness and so nothing can work on
St. Swithin's day.
More information about the Python-list
mailing list