1.classes&objects
a class combines (and abstracts)data and functions,serves as a template for its insataances.
an object is an instantiation of aclass
e.g. string is a buit-in class, append is a function
int is a built in class, + is a function
2.object-oriented programming
a method for organizing modular programs
absraction barriers
bundling together information and related behavior
a metaphor for computatiion using distributed state
each object has its own local state
each object also knows how to manage its own local state, based on method calls
method calls are messaages passed between objects
several objects may all be instances of a common type.
different types may relate to each other
specialized syntax &vocabulary to support this metaphor
3.the class statement
a class statement creates anew class and binds that class to <name> in the first frame of the current environment.
aissignment & def statements in <suite.> create attributes of the class (not names in frames)
when a class is called:
1. a new instance of that class is created:
2. The __init__ method of the class is called with the new object as its first argument (named self).along with any additional arguments provided in the call expression.
4.object identity
every object that is an instance of a user-defined clas has a unique identity:
identity operators "is" and "is not" test if two expressions evaluate to the same object
binding an object to a new name using assignment does not create a new object
e.g.
>>> list1 = [1, 2, 3]
>>> list2 = list1
>>> list2.append(9)
>>> list2
[1, 2, 3, 9]
>>> list1
[1, 2, 3, 9]
but
>>>a = 1
>>>b = a
>>>c = 1
>>>a is c
True
>>>a = 2
>>>a is b
False
5.methods
methods are defined in th esuite of a class statement
6.invoking methods
7.dot expressions
objects receive messages via dot notation
dot notation accesses attributes of the instance or its class.
8.Attributes
getattr() : the same as dot expressions
9.Methods and functions
Object + Function = Bound Method
本文概述了面向对象编程中的关键概念,包括类和对象的关系、方法组织、类声明、对象身份、方法调用、点表示法以及属性的访问。特别强调了新实例化、__init__方法、对象绑定和方法与函数的绑定等重要特性。

被折叠的 条评论
为什么被折叠?



