具有ToString功能的枚举类 [英] Enum class with ToString functionality
问题描述
我有以下课程 -
class TestOutcomes:
PASSED = 0
FAILED = 1
ABORTED = 2
加上以下代码 -
testResult = TestOutcomes.PASSED
testResultAsString
if testResult == TestOutcomes.PASSED:
testResultAsString =" Passed"
elif testResult == TestOutcomes.FAILED:
testResultAsString =" Failed"
else:
testResultAsString ="中止了
但是,如果我有一个函数来转换为字符串,那么它会更好。
是TestOutcomes类的一部分。我该如何实现呢?
谢谢,
Barry
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?
Thanks,
Barry
推荐答案
bg***@yahoo.com 写道:
bg***@yahoo.com wrote:
但如果我有一个函数来转换为字符串,那么它将会更好。
是TestOutcomes类的一部分。我该如何实现?
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?
也许: http://aspn.activestate.com/ASPN/Coo.../Recipe/413486
2007年10月9日, bg***@yahoo.com < bg *** @ yahoo.comwrote:
On 9/10/07, bg***@yahoo.com <bg***@yahoo.comwrote:
我有以下课程 -
class TestOutcomes:
PASSED = 0
FAILED = 1
ABORTED = 2
加上以下代码 -
testResult = TestOutcomes.PASSED
testResultAsString
if testResult == TestOutcomes.PASSED:
testResultAsString =" Passed" ;
elif testResult == TestOutcomes.FAILED:
testResultAsString =" Failed"
else:
testResultAsString =已中止
但它会更好如果我有一个函数来转换为字符串作为TestOutcomes类的一部分。我该如何实现?
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?
您可以使用反射来执行此操作。也许是通过你的类dict(dir(self))获得
的构造函数,并根据属性的值和名称构建一个int->字符串映射
属性(也许只是
类型为int的那些)。要获得与您的代码完全相同的结果
,您可以将属性名称的第一个字母大写,并且
小写其余部分。
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.
9月10日凌晨2:28,bg ... @ yahoo.com写道:
On Sep 10, 2:28 am, bg...@yahoo.com wrote:
我有以下课程 -
class TestOutcomes:
PASSED = 0
FAILED = 1
ABORTED = 2
加上以下代码 -
testResult = TestOutcomes.PASSED
testResultAsString
if testResult == TestOutcomes.PASSED:
testResultAsString =" Passed"
elif testResult == TestOutcomes .FAILED:
testResultAsString =" Failed"
else:
testResultAsString =" aborted"
但是如果我有一个函数来转换为字符串,那么它将会更好。
是TestOutcomes类的一部分。我将如何实现这个?
谢谢,
Barry
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?
Thanks,
Barry
相当于Java'的toString()是Python中的__str __():
class TestOutcomes:
PASSED = 0
FAILED = 1
ABORTED = 2
def __init __(自我,结果):
self.outcome = results
def __str __(自我):
if self.outcome == TestOutcomes.PASSED:
return" Passed"
elif self.outcome == TestOutcomes.FAILED:
return" Failed"
else:
return" Aborted"
if __name__ ==" __ main __":
testResult = TestOutcomes(TestOutcomes.ABORTED)
print testResult >
testResult = TestOutcomes(TestOutcomes.FAILED)
a = testResult .__ str __()
打印一份
已中止
失败
The equivalent to Java''s toString() is __str__() in Python:
class TestOutcomes:
PASSED = 0
FAILED = 1
ABORTED = 2
def __init__(self,outcome):
self.outcome = outcome
def __str__(self):
if self.outcome == TestOutcomes.PASSED:
return "Passed"
elif self.outcome == TestOutcomes.FAILED :
return "Failed"
else:
return "Aborted"
if __name__ == "__main__":
testResult = TestOutcomes(TestOutcomes.ABORTED)
print testResult
testResult = TestOutcomes(TestOutcomes.FAILED)
a = testResult.__str__()
print a
Aborted
Failed
这篇关于具有ToString功能的枚举类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!