文章目录
- Python for Everybody
- 课程简介
- Quiz 15: Object-Oriented Programming
- 单选题(1-11)
- Multiple instances
Python for Everybody
课程简介
Python for Everybody 零基础程序设计(Python 入门)
- This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
- We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
- The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
- This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
- This course covers Python 3.
coursera
Python for Everybody 零基础程序设计(Python 入门)
Charles Russell Severance
Clinical Professor
个人主页
Twitter
University of Michigan
课程资源
coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站
PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网
Quiz 15: Object-Oriented Programming
We do a quick look at how Python supports the Object-Oriented programming pattern.
单选题(1-11)
- Which came first, the instance or the class?
- method
- class
- instance
- function
- In Object Oriented Programming, what is another name for the attributes of an object?
- portions
- messages
- fields
- forms
- methods
- Which of the following is NOT a good synonym for “class” in Python?
- blueprint
- template
- pattern
- direction
- What does this Python statement do if PartyAnimal is a class?
zap = PartyAnimal()
- Use the PartyAnimal template to make a new object and assign it to zap
- Copy the value from the PartyAnimal variable to the variable zap
- Subtract the value of the zap variable from the value in the PartyAnimal variable and put the difference in zap
- Clear out all the data in the PartyAnimal variable and put the old values for the data in zap
- What is the syntax to look up the fullname attribute in an object stored in the variable colleen?
- $colleen->fullname
- c o l l e e n − > colleen-> colleen−>fullname
- $colleen::fullname
- colleen.fullname
- $colleen[“fullname”]
- Which of the following statements are used to indicate that class A will inherit all the features of class B?
- class A(B):
- A=B++;
- class A extends B:
- class A inherits B:
- class A instanceOf B;
- How is “self” typically used in a Python method within a class?
- The number of parameters to the method
- To set the residual value in an expression where the method is used
- To terminate a loop
- To refer to the instance in which the method is being called
- What does the Python dir() function show when we pass an object into it as a parameter?
- It shows the methods and attributes of an object
- It shows the type of the object
- It shows the parent class
- It shows the number of parameters to the constructor
- Which of the following is rarely used in Object Oriented Programming?
- Attribute
- Inheritance
- Destructor
- Method
- Constructor
Multiple instances
So far, we have defined a class, constructed a single object, used that object, and then thrown the object away. However, the real power in object-oriented programming happens when we construct multiple instances of our class.
When we construct multiple objects from our class, we might want to set up different initial values for each of the objects. We can pass data to the constructors to give each object a different initial value:
class PartyAnimal:
x = 0
name = ''
def __init__(self, nam):
self.name = nam
print(self.name,'constructed')
def party(self) :
self.x = self.x + 1
print(self.name,'party count',self.x)
s = PartyAnimal('Sally')
j = PartyAnimal('Jim')
s.party()
j.party()
s.party()
# Code: http://www.py4e.com/code3/party5.py
The constructor has both a self
parameter that points to the object instance and additional parameters that are passed into the constructor as the object is constructed:
s = PartyAnimal('Sally')
Within the constructor, the second line copies the parameter (nam
) that is passed into the name
attribute within the object instance.
self.name = nam
The output of the program shows that each of the objects (s
and j
) contain their own independent copies of x
and nam
:
Sally constructed
Jim constructed
Sally party count 1
Jim party count 1
Sally party count 2