定义类

  1. class Point:
  2. def __init__(self, x, y):
  3. self.x = x
  4. self.y = y
  5. def __str__(self):
  6. return "{} and {}".format(self.x, self.y)
  7. def show(self):
  8. print(self.x, self.y)

dict

  1. >>> p = Point(4, 5)
  2. >>> print(p)
  3. 4 and 5
  4. >>> print(p.__dict__)
  5. {'x': 4, 'y': 5}
  6. >>> p.__dict__['y'] = 16
  7. >>> print(p.__dict__)
  8. {'x': 4, 'y': 16}