这可能是微不足道的,但搜索这个时候我不分辨率:Python不显示类的打印报表,结果
我有以下简单的类:
class Celcius:
def __init__(self, temperature=0):
self.temperature = temperature
def to_fahrenheit(self):
return (self.temperature*1.8) + 32
def get_temperature(self):
print "getting temp"
return self._temperature
def set_temperature(self, value):
if value < -273:
raise ValueError("dayum u trippin' fool")
print "setting temp"
self._temperature = value
temperature = property(get_temperature, set_temperature)
c = Celcius()
当我运行这个在Sublime Text 3中(通过点击cmd + B)控制台不会打印任何东西。我应该看到:
setting temp
如果我添加以下到脚本的末尾:预期
print "banana"
print "apple"
两行打印。
如果我从终端(使用python -u或python)运行上面的python脚本,结果是完全一样的。我想我错过了一些非常愚蠢的事情。谢谢
啊,那工作!谢谢。所以我应该总是从'object'固有才是安全的? – luffe
斑点。 =) – khelwood
@luffe:阅读词汇表中的[“新风格类”](https://docs.python.org/2/glossary.html#term-new-style-class)。在Python 3中,你总是有新式的类,所以你的代码会在没有修改的情况下运行。 – Matthias