Skip to main content
 首页 » 编程设计

Python 不显示类中打印语句的结果

2025年05月04日12EasonJim

<分区>

这可能是微不足道的,但我在搜索时没有得到解决:

我有以下简单的类:

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 脚本(使用 python -u,或者只是 python),结果是完全一样的。我想我错过了一些非常愚蠢的东西。谢谢