类(class):一种程序员自定义的类型。类定义创建了一个新的类对象。
类对象(class object):包含程序员自定义类型的细节信息的对象。类对象可以被用于创建该类型的实例。
实例(instance):属于某个类的对象。
实例化(instantiate):创建新的对象。
属性(attribute):和某个对象相关联的有命名的值。
嵌套对象(embedded object):作为另一个对象的属性存储的对象。
浅复制(shallow copy):在复制对象内容的时候,只包含嵌套对象的引用,通过 copy 模块的 copy 函数实现。
深复制(deep copy):在复制对象内容的时候,既复制对象属性,也复制所有嵌套对象及其中的所有嵌套对象,由 copy 模块的 deepcopy 函数实现。
对象图(object diagram):展示对象及其属性和属性值的图。

'''
#定义一个对象
class Point:
  """Represents a point in 2-D space."""

blank=Point()
blank.x=3.0
blank.y=4.0
'(%g, %g)' % (blank.x, blank.y)

import math
distance=math.sqrt(blank.x**2+blank.y**2)
print(distance)
def print_point(p):
  print('(%g,%g)' % (p.x,p.y))
print(print_point(blank))
'''

#定义一个矩形类
class Point:
  """Represents a point in 2-D space."""

class Rectangle:
  """Represents a rectangle.

  attributes: width, height, corner.
  """
def print_point(p):
  print('(%g,%g)' % (p.x,p.y))

box=Rectangle()
box.width=100.00
box.height=200.00
box.corner=Point()
box.corner.x=0.0
box.corner.y=0.0
squre=box.width*box.height
print("矩形面积是:",squre)

#实例作为返回值
def find_center(rect):
  p=Point()
  p.x=rect.corner.x+rect.width/2
  p.y=rect.corner.y+rect.height/2
  return p

center=find_center(box)
print(print_point(center))
#返回值为None,而不是一个坐标,why

#对象是可变的
box.width=box.width+50
box.height=box.height+50

def grow_rectangle(rect,dwidth,dheight):
  rect.width+=dwidth
  rect.height+=dheight

print(box.width,box.height)

grow_rectangle(box,50,100)
print(box.width,box.height)

import copy
p1=Point()
p1.x=3.0
p1.y=4.0
p2=copy.copy(p1)
print(p1 is p2) #False
print(p1==p2) #False
box2=copy.copy(box)
print(box2 is box) #False
print(box2.corner is box.corner) #True


p=Point()
p.x=3
p.y=4
print(type(box2)) #查询box2的类型。
isinstance(p, Point) #用 isinstance 来检查某个对象是不是某个类的实例
#内置函数 hasattr 检查一个对象是否拥有某个属性
hasattr(p, 'x') #True,第一个参数可以是任何对象; 第二个参数是一个字符串,代表了某个属性的名字。
hasattr(p, 'z') #False,第一个参数可以是任何对象; 第二个参数是一个字符串,代表了某个属性的名字。
#也可以使用try except 来检查某个对象是不是你需要的属性。如:
try:
  x=p.x
except AttributeError:
  x=0