2017-03-15 21 views
2

我已经将下面的代码作为赋值的一部分。super()在Sublime Text中抛出一个错误,在PyCharm/Terminal中工作

class Question: 
    """Base class for all questions""" 

    question_count = 0 

    def __init__(self, desc): 
     self.desc = desc 

     Question.question_count += 1 


class MarkovMM(Question): 
    def __init__(self, desc, arrival, service): 
     super().__init__(desc) 
     if self.desc == "Question 2": 
      self.answer = round(1 - (1 - (arrival/service)) - ((1 - (arrival/service)) * (arrival/service)), 3) 
     elif self.desc == "Question 3": 
      self.answer = round(1/((service/60) - (arrival/60)), 4) 

qu2 = MarkovMM("Question 2", 5, 23) 
print(qu2.answer) 
qu3 = MarkovMM("Question 3", 6, 22) 
print(qu3.answer) 

当我通过PyCharm和Ubuntu终端运行它时,它工作得很好。但是,在Sublime Text中运行它会产生以下错误。

Traceback (most recent call last): 
    File "/home/estilen/Dropbox/College/Year_3/CSA2/Python/hello.py", line 20, in <module> 
    qu2 = MarkovMM("Question 2", 5, 23) 
    File "/home/estilen/Dropbox/College/Year_3/CSA2/Python/hello.py", line 14, in __init__ 
    super().__init__(desc) 
TypeError: super() takes at least 1 argument (0 given) 

为什么错误出现在Sublime中,但不是在PyCharm或Terminal中?

所需的输出:

0.047 
3.75 
+3

因为你的'Sublime'可能指向一个Python3版本,而终端和PyCharm正在调用Python2。 – Abdou

+0

@Abdou我的意思是使用Python 3. –

+0

好像Sublime已经指向Python3,如果代码片段在那里运行没有任何问题。也许PyCharm和你的终端可能需要这里的设置。 – Abdou

回答

6

你sublimetext使用默认的编译系统,这是Python的2.配置能在Python运行3

Tools -> Build System -> New Build System ...

添加这些内容:

{ 
    "cmd": ["python3", "-u", "$file"], 
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", 
    "selector": "source.python" 
} 

用合理的fi保存配置例如python3.sublime-build,并在Tools -> Build With ...中选择这个新创建的版本。

+0

工程就像一个魅力!谢谢你,先生。另外,为了解决另一个问题,如果错误是由于Python版本的不同而引发的,Python 2和Python 3中的super()之间的核心区别是什么? –

+1

最初,您必须在调用super时指定类型。在Python 3中,他们添加了一些[黑魔法](http://stackoverflow.com/q/36993577/674039)来移除这个样板。本着对Python 2的强烈爱好的精神,没有人会反过来支持这种语法糖,所以如果你使用Python 2,你仍然必须使用糟糕的旧版本。 – wim

+0

@wim:https:// stackoverflow中有一个注释。 com/documentation/python/419/classes/1399/basic-inheritance#t = 201703160319593873367可以扩展。 – Ryan

相关问题