牢记有关变量的规则php
greeting_message
。name
比 n 好,student_name
比 s_n 好,name_length
比 length_of_persons_name 好。traceback是一条记录,指出了解释器尝试运行代码时,在什么地方陷入了困境。
要理解新的编程概念,最佳的方式是尝试在程序中使用它们。java
代码:python
name = "ada lovelace"
print name.title()
print name.upper()
print name.lower()
输出结果:程序员
Ada Lovelace ADA LOVELACE ada lovelace
str.rstrip([chars]) # 剔除字符串结尾的指定字符(默认为空白)
lstrip([chars]) # 剔除字符串开头的指定字符(默认为空白)
strip([chars]) # 同时剔除字符串开头和结尾的指定字符(默认为空白)
在Python 2中,无需将打印的内容放在括号内。
从技术上说,Python 3中的print
是一个函数,所以括号必不可少。sql
Python 2中,整数除法的结果只包含整数部分,小数部分被删除,注意不是四舍五入,而是直接删除小数部分。
Python 2中,要避免这种状况,务必确保至少有一个操做为浮点数,这样结果也将为浮点数。编程
当前,大多数软件都是合做编写的,编写者多是同一家公司的多名员工,也多是众多致力于同一个开源项目的人员。训练有素的程序员都但愿代码中包含注释,所以最好从如今开始就在程序中添加描述性注释。做为新手,最值得养成的习惯之一是,在代码中编写清晰、简洁的注释。
若是不肯定是否要编写注释,就问问本身,找到合理的解决方案前,是否考虑了多个解决方案。若是答案是确定的,就编写注释对的解决方案进行说明吧。相比回过头去再添加注释,删除多余的注释要容易得多。json
在解释器中执行命令import this
就会显示Tim Peters的The Zen of python:ruby
>>>import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
motorcycles = ['honda', 'yamaha', 'suzuki']
print (motorcycles)
motorcycles[0] = 'ducati'
print (motorcycles)
输出:bash
['honda', 'yamaha', 'suzuki'] ['ducati', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
输出:markdown
['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha', 'suzuki', 'ducati']
insert()
能够在列表的任何位置添加新元素。须要制定新元素的索引和值。motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
这种操做将列表既有的每一个元素都右移一个位置:
['ducati', 'honda', 'yamaha', 'suzuki']
能够根据位置或值来删除列表中的元素。
del
语句删除元素motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
输出:
['honda', 'yamaha', 'suzuki'] ['yamaha', 'suzuki']
pop()
删除元素 pop()
可删除列表末尾的元素,并可以接着使用它。motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
输出:
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
suzuki
pop()
方法来删除列表中任何位置的元素,只需在括号中指定要删除的元素的索引便可。motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
输出:
The first motorcycle I owned was a Honda.
remove()
。motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
输出:
['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki']
使用remove()
从列表中删除元素时,也可接着使用它的值
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
值’ducati’已经从列表中删除,但它还存储在变量too_expensive
中
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
A Ducati is too expensive for me.
方法remove()
只删除第一个指定的值。若是要删除的值可能在列表中出现屡次,就须要使用循环来判断是否删除了全部这样的值。
sort()
对列表进行永久性排序cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
永久性排序:
['audi', 'bmw', 'subaru', 'toyota']
能够按与字母顺序相反的顺序排列列表元素,为此,只需向sort()
方法传递参数reverse=True
。
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
一样是永久性修改列表元素顺序:
['toyota', 'subaru', 'bmw', 'audi']
sorted()
对列表进行临时排序cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)
输出:
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']
Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']
Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']
若是要按与字母顺序相反的顺序显示列表,也可向函数sorted()
传递参数reverse=True
。
注意 在并不是全部的值都是小写时,按字母顺序排列列表要复杂些。决定排列顺序时,有多种解读大写字母的方式,要指定准确的排列顺序,可能比咱们这里所作的要复杂。
要反转列表元素的排列顺序,可以使用方法reverse()
。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
注意,reverse()
不是指按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序:
['bmw', 'audi', 'toyota', 'subaru'] ['subaru', 'toyota', 'audi', 'bmw']
方法reverse()
永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,为此只需对列表再次调用reverse() 便可。
假设有一个包含三个元素的列表,却要求获取第四个元素:
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])
这将致使索引错误 :
Traceback (most recent call last): File "motorcycles.py", line 3, in <module> print(motorcycles[3]) IndexError: list index out of range
索引错误意味着Python没法理解指定的索引。程序发生索引错误时,请尝试将指定的索引减1,而后再次运行程序,看看结果是否正确。
每当须要访问最后一个列表元素时,均可使用索引-1 。这在任何状况下都行之有效,即使最后一次访问列表后,其长度发生了变化。
for value in range(1, 6):
print(value)
注意输出不包含第二个值:
1 2 3 4 5
函数range()
还可指定步长
语法:
range(start, stop[, step])
输入:
even_numbers = list(range(2,11,2))
print(even_numbers)
输出:
[2, 4, 6, 8, 10]
min() max() sum()
列表解析将for循环和建立新元素的代码合并成一行,并自动附加新元素。
squares = [value**2 for value in range(1,11)]
print(squares)
输出:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
等同于:
squares = []
for value in range(1, 11):
squares.append(value**2)
print(squares)
要复制列表,可建立一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:]
)。
咱们在不指定任何索引的状况下从列表my_foods
中提取一个切片,从而建立了这个列表的副本,再将该副本存储到变量friend_foods
中。
核实咱们确实有两个列表:
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
输出:
My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
假若咱们只是简单地将my_foods
赋给friend_foods
,就不能获得两个列表。
列表很是适合用于存储在程序运行期间可能变化的数据集。
列表是能够修改的,这对处理网站的用户列表或游戏中的角色列表相当重要。然而,有时候须要建立一系列不可修改的元素,元组能够知足这种需求。
Python将不能修改的值称为不可变的 ,而不可变的列表被称为元组 。
元组看起来犹如列表,但使用圆括号而不是方括号来标识。
dimensions = (200, 50)
dimensions[0] = 250
输出:
Traceback (most recent call last): File "dimensions.py", line 3, in <module> dimensions[0] = 250 TypeError: 'tuple' object does not support item assignment
代码试图修改第一个元素的值,致使Python返回类型错误消息。因为试图修改元组的操做是被禁止的,所以Python指出不能给元组的元素赋值。
可以使用for循环来遍历元组中的全部值。
相比于列表,元组是更简单的数据结构。若是须要存储的一组值在程序的整个生命周期内都不变,可以使用元组。
PEP 8是最古老的PEP(Python Enhancement Proposal,PEP)之一,它向Python程序员提供了代码格式设置指南。PEP 8的篇幅很长,但大都与复杂的编码结构相关。
若是必定要在让代码易于编写和易于阅读之间作出选择,Python程序员几乎老是会选择后者。
不少Python程序员都建议每行不超过80字符。
PEP 8还建议注释的行长都不超过72字符,由于有些工具为大型项目自动生成文档时,会在每行注释开头添加格式化字符。
PEP 8中有关行长的指南并不是不可逾越的红线,有些小组将最大行长设置为99字符。在学习期间,不用过多地考虑代码的行长,但别忘了,协做编写程序时,你们几乎都遵照PEP 8指南。在大多数编辑器中,均可设置一个视觉标志——一般是一条竖线,让你知道不能越过的界线在什么地方。
else 语句指定了条件测试未经过时要执行的操做。
常常须要检查超过两个的情形,为此可以使用Python提供的if-elif-else
结构。
Python只执行if-elif-else
结构中的一个代码块,它依次检查每一个条件测试,直到遇到经过了的条件测试。
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $5.")
else:
print("Your admission cost is $10.")
可根据须要使用任意数量的elif
代码块:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print("Your admission cost is $" + str(price) + ".")
Python并不要求if-elif 结构后面必须有else 代码块。在有些状况下,else 代码块颇有用;而在其余一些状况下,使用一条elif 语句来处理特定的情形更清晰。
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 5
print("Your admission cost is $" + str(price) + ".")
else 是一条一应俱全的语句,只要不知足任何if 或elif 中的条件测试,其中的代码就会执行,这可能会引入无效甚至恶意的数据。若是知道最终要测试的条件,应考虑使用一个elif 代码块来代替else 代码块。这样就能够确定,仅当知足相应的条件时,代码才会执行。
可将任何Python对象用做字典中的值。
字典是一种动态结构,可随时在其中添加键-值对。要添加键-值对,可依次指定字典名、用方括号括起的键和相关联的值。
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
输出:
{'color': 'green', 'points': 5}
{'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
使用字典来存储用户提供的数据或在编写能自动生成大量键-值对的代码时,一般都须要先定义一个空字典。
alien_0 = {'color': 'green'}
print("The alien is " + alien_0['color'] + ".")
alien_0['color'] = 'yellow' # 修改值
print("The alien is now " + alien_0['color'] + ".")
输出:
The alien is green.
The alien is now yellow.
可以使用del 语句将相应的键-值对完全删除,使用del 语句时,必须指定字典名和要删除的键。
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
del alien_0['points']
print(alien_0)
输出:
{'color': 'green', 'points': 5}
{'color': 'green'}
肯定须要使用多行来定义字典时,在输入左花括号后按回车键,再在下一行缩进四个空格,指定第一个键-值对,并在它后面加上一个逗号。此后再次按回车键时,文本编辑器将自动缩进后续键-值对,且缩进量与第一个键-值对相同。
定义好字典后,在最后一个键-值对的下一行添加一个右花括号,并缩进四个空格,使其与字典中的键对齐。另一种不错的作法是在最后一个键-值对后面也加上逗号,为之后在下一行添加键-值对作好准备。
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user_0.items(): # for语句的第二部分包含字典名和方法items()
print("\nKey: " + key)
print("Value: " + value)
输出:
Key: last
Value: fermi
Key: first Value: enrico Key: username Value: efermi
经过“+”链接符链接key
和value
:
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby'
'phil': 'python',
}
for name, language in favorite_languages.items():
print(name.title() + "'s favorite language is " + language.title() + ".")
输出:
Jen's favorite language is Python.
Sarah's favorite language is C.
Phil's favorite language is Python.
Edward's favorite language is Ruby.
在不须要使用字典中的值时,方法keys()
颇有用。
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
for name in favorite_languages.keys():
print(name.title())
输出:
Jen Sarah Phil Edward
遍历字典时,会默认遍历全部的键,所以,若是将上述代码中的for name in favorite_languages.keys():
替换为 for name in favorite_languages:
,输出将不变。
若是显式地使用方法keys()
可以让代码更容易理解,你能够选择这样作,但若是你愿意,也可省略它。
favorite_languages = {
'jen': 'python',
'edward': 'ruby',
'phil': 'python',
}
friends = ['phil', 'sarah']
for name in favorite_languages.keys():
print(name.title())
if name in friends:
print("Hi " + name.title() + ", I see your favorite language is " + favorite_languages[name].title() + "!")
输出:
Edward
Jen
Phil
Hi Phil, I see your favorite language is Python!
input()
函数input()
接受一个参数:即要向用户显示的提示 或说明,让用户知道该如何作。
个性化打招呼:
name = input("Please enter your name: ")
print("Hello, " + name + "!")
int()
来获取数值输入height = input("How tall are you, in inches? ")
height = int(height)
if height >= 36:
print("\nYou're tall enough to ride!")
else:
print("\nYou'll be able to ride when you're a little older.")
输出:
How tall are you, in inches? 71
You're tall enough to ride!
将数值输入用于计算和比较前,务必将其转换为数值表示。
若是使用的是Python 2.7,应使用函数raw_input()
来提示用户输入。这个函数与Python 3中的input()
同样,也将输入解读为字符串。
Python 2.7也包含函数input()
,但它将用户输入解读为Python代码,并尝试运行它们。所以,最好的结果是出现错误,指出Python不明白输入的代码;而最糟的结果是,将运行本来无心运行的代码。
咱们在其中定义了一个退出值,只要用户输入的不是这个值,程序就接着运行:
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. Hello everyone!
Hello everyone!
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. Hello again.
Hello again.
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program. quit
代码:
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
message = ""
while message != 'quit':
message = raw_input(prompt) # 注意raw_input()已包含print(prompt)步骤
#print(message)
if message == 'quit':
print(message)
在要求不少条件都知足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志,充当了程序的交通讯号灯。可以让程序在标志为True
时继续运行,并在任何事件致使标志的值为False
时让程序中止运行。这样,在while语句中就只需检查一个条件——标志的当前值是否为True
,并将全部测试(是否发生了应将标志设置为False
的事件)都放在其余地方,从而让程序变得更为整洁。
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
break
退出循环要当即退出while循环,再也不运行循环中余下的代码,也无论条件测试的结果如何,可以使用break语句。
注意: 在任何Python循环中均可使用break语句。例如,可以使用break语句来退出遍历列表或字典的for循环。
continue
要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可以使用continue语句,它不像break语句那样再也不执行余下的代码并退出整个循环。
例如,来看一个从1数到10,但只打印其中偶数的循环:
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue #忽略余下的代码,并返回到循环的开头
print(current_number)
输出:
1 3 5 7 9
每一个while循环都必须有中止运行的途径,这样才不会没完没了地执行下去。
要避免编写无限循环,务必对每一个while循环进行测试,确保它按预期那样结束。若是但愿程序在用户输入特定值时结束,可运行程序并输入这样的值;若是在这种状况下程序没有结束,请检查程序处理这个值的方式,确认程序至少有一个这样的地方能让循环条件为False
或让break语句得以执行。
for循环是一种遍历列表的有效方式,但在for循环中不该修改列表,不然将致使Python难以跟踪其中的元素。
要在遍历列表的同时对其进行修改,可以使用while循环。经过将while循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供之后查看和显示。
假设有一个宠物列表,其中包含多个值为’cat’的元素。要删除全部这些元素,可不断运行一个while循环,直到列表中再也不包含值’cat’,以下所示:
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)
while 'cat' in pets:
pets.remove('cat') #list.remove(obj)移除列表中某个值的第一个匹配项
print(pets)
输出:
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] ['dog', 'dog', 'goldfish', 'rabbit']
建立一个调查程序,其中的循环每次执行时都提示输入被调查者的名字和回答。咱们将收集的数据存储在一个字典中,以便将回答同被调查者关联起来:
注意python2 中用raw_input()
,python3 中input()
responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = raw_input("\nWhat is your name? ")
response = raw_input("Which mountain would you like to climb someday? ")
# 将答卷存储在字典中
responses[name] = response
# 看看是否还有人要参与调查
repeat = raw_input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = False
# 调查结束,显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")
输出:
What is your name? Eric
Which mountain would you like to climb someday? Denali
Would you like to let another person respond? (yes/ no) yes
What is your name? Lynn
Which mountain would you like to climb someday? Devil's Thumb
Would you like to let another person respond? (yes/ no) no
--- Poll Results ---
Lynn would like to climb Devil's Thumb.
Eric would like to climb Denali.
有时候,须要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种状况下,可将函数编写成可以接受任意数量的键-值对——调用语句提供了多少就接受多少。
def build_profile(first, last, **user_info):
"""建立一个字典,其中包含咱们知道的有关用户的一切"""
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key, value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('albert', 'einstein',
location='princeton',
field='physics')
print(user_profile)
输出:
{'field': 'physics', 'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton'}
形参**user_info
中的两个星号让Python建立一个名为user_info
的空字典,并将收到的全部名称-值对都封装到这个字典中。在这个函数中,能够像访问其余字典那样访问user_info
中的名称—值对。
在这里,返回的字典包含用户的名和姓,还有求学的地方和所学专业。编写函数时,能够以各类方式混合使用位置实参、关键字实参和任意数量的实参。
将函数存储在被称为模块的独立文件中,再将模块导入到主程序中,import
语句容许在当前运行的程序文件中使用模块中的代码。
经过将函数存储在独立的文件中,可隐藏程序代码的细节,将重点放在程序的高层逻辑上。
这还能让你在众多不一样的程序中重用函数。将函数存储在独立文件中后,可与其余程序员共享这些文件而不是整个程序。
知道如何导入函数还能让你使用其余程序员编写的函数库。
import module_name
若是使用import
语句导入了名为module_name.py的整个模块,就可以使用下面的语法来使用其中任何一个函数:
module_name.function_name()
from module_name import function_name
经过用逗号分隔函数名,可根据须要从模块中导入任意数量的函数:
from module_name import function_0, function_1, function_2
若使用这种语法,调用函数时就无需使用句点。因为在import
语句中显式地导入了函数function_name()
,所以调用它时只需指定其名称。
若是要导入的函数的名称可能与程序中现有的名称冲突,或者函数的名称太长,可指定简短而独一无二的别名——函数的另外一个名称,相似于外号。要给函数指定这种特殊外号,须要在导入它时这样作。
下面给函数make_pizza()
指定了别名mp()
。这是在import
语句中使用make_pizza as mp
实现的,关键字as
将函数重命名为你提供的别名:
from pizza import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')
上面的import语句将函数make_pizza()
重命名为mp()
;在这个程序中,每当须要调用make_pizza()
时,均可简写成mp()
,而Python将运行make_pizza()
中的代码,这可避免与这个程序可能包含的函数make_pizza()
混淆。
指定别名的通用语法以下:
from module_name import function_name as fn
给模块指定别名的通用语法:
import module_name as mn
使用星号(*)运算符可以让Python导入模块中的全部函数。
因为导入了每一个函数,可经过名称来调用每一个函数,而无需使用句点表示法。
然而,使用并不是本身编写的大型模块时,最好不要采用这种导入方法:若是模块中有函数的名称与你的项目中使用的名称相同,可能致使意想不到的结果:Python可能遇到多个名称相同的函数或变量,进而覆盖函数,而不是分别导入全部的函数。
最佳的作法是,要么只导入你须要使用的函数,要么导入整个模块并使用句点表示法。这能让代码更清晰,更容易阅读和理解。
之因此介绍这种导入方法,只是想让你在阅读别人编写的代码时,若是遇到相似于下面的import语句,可以理解它们:
from module_name import *
# 不推荐使用该种导入方法!!!
import
语句都应放在文件开头,惟一例外的情形是,在文件开头使用了注释来描述整个程序。__init__()
"""electric_car.py"""
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.modes
return long_name.title()
def read_odometer(self):
print("This car has " + str(self.odometer_reading) + " miles on in.")
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())
建立子类时,父类必须包含在当前文件中,且位于子类前面。
定义子类时,必须在括号内指定父类的名称。ElectricCar中方法__init__()
接受建立Car实例所需的信息。
super()
是一个特殊函数,帮助Python将父类和子类关联起来。这行代码让Python调用ElectricCar的父类的方法__init__()
,让ElectricCar实例包含父类的全部属性。父类也称为超类(superclass),名称super所以而得名。
在Python 2.7中,继承语法稍有不一样,ElectricCar类的定义相似于下面这样:
class Car(object):
def __init__(self, make, model, year):
--snip--
class ElectricCar(Car):
def __init__(self, make, model, year):
super(ElectricCar, self).__init__(make, model, year)
--snip--
函数super()
须要两个实参:子类名和对象self。为帮助Python将父类和子类关联起来,这些实参必不可少。
另外,在Python 2.7中使用继承时,务必在定义父类时在括号内指定object
。
对于父类的方法,只要它不符合子类模拟的实物的行为,均可对其进行重写。为此,可在子类中定义一个这样的方法,即它与要重写的父类方法同名。这样,Python将不会考虑这个父类方法,而只关注你在子类中定义的相应方法。
假设Car类有一个名为fill_gas_tank()
的方法,它对全电动汽车来讲毫无心义,所以你可能想重写它。
def ElectricCar(Car):
--snip-
def fill_gas_tank():
"""电动汽车没有油箱"""
print("This car doesn't need a gas tank!")
如今,若是有人对电动汽车调用方法fill_gas_tank()
,Python将忽略Car类中的方法fill_gas_tank()
,转而运行上述代码。使用继承时,可以让子类保留从父类那里继承而来的精华,并剔除不须要的糟粕。