【发布时间】:2011-03-20 15:20:23
【问题描述】:
在以 OOP 方式工作时,在 Python 中经常测试类型值吗?
class Foo():
def __init__(self,barObject):
self.bar = setBarObject(barObject)
def setBarObject(barObject);
if (isInstance(barObject,Bar):
self.bar = barObject
else:
# throw exception, log, etc.
class Bar():
pass
或者我可以使用更宽松的方法,例如:
class Foo():
def __init__(self,barObject):
self.bar = barObject
class Bar():
pass
【问题讨论】:
标签: python oop introspection