python编程 从入门到实践 9.3 TypeError

TypeError

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Car():
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0

def get_descriptive_name(self):
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()

def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on it.")

def update_odometer(self, mileage):
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")

def increment_odometer(self, miles):
self.odometer_reading += miles

class ElectricCar(Car):
def __init__(self, make, model, year):
super().__init__(make, model, year)

my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())

直接在sublime text运行会出现错误:

1
2
3
4
5
6
Traceback (most recent call last):
File "D:\python_work\electric_car.py", line 28, in <module>
my_tesla = ElectricCar('tesla', 'model s', 2016)
File "D:\python_work\electric_car.py", line 26, in __init__
super().__init__(make, model, year)
TypeError: super() takes at least 1 argument (0 given)

因为sublime text默认是Python2,所以我的vs code和pycharm都不会出现这个问题。

既然其他编辑器能正常运行,问题就只是出在sublime text上,那修改方法如下:

第1种

安装https://packagecontrol.io/packages/Python%203的python3:

sb1

注意这个的版本是2017年的。

具体步骤

  1. 先安装 Package Control
  2. 打开 ToolsCommand Palette
  3. 选择 Package Control: Install Package

sb2

  1. 搜索Python 3Cython+ ,找到并安装

sb3

sb5

  1. 修改View → Syntax → Open all with current extension as… →为 Python 3

sb4

如何安装Package Control

如果你没有安装过Package Control:

  1. 点击 Preferences > Browse Packages… menu
  2. Browse up a folder and then into the Installed Packages/ folder
  3. 下载 Package Control.sublime-package 然后复制到 Installed Packages/ 目录
  4. 重启 Sublime Text

第2种(推荐)

找到电脑里的python路径:

sb6

配置sublime text里运行python的环境:

打开 ToolsBuild SystemNew Build System...

1
2
3
4
5
{
"cmd": ["C:/Users/dell/AppData/Local/Programs/Python/Python36/Python.exe", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

另存为Python3.6.8.sublime-build,然后在修改ToolBuild System →为 python 3.6.8

sb7

sb8