On 9/10/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hi,
>
> 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?

You can use reflection to do this. Perhaps a constructor that goes
through your class dict (dir(self)) and builds an int->string mapping
property based on the value and name of the attributes (perhaps just
the ones whose type is int). To get the exact same result as your code
above you can capitalize the first letter of the attribute name, and
lowercase the rest.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to