# point.py
class Point:
def __init__(self, x=0, y=0):
self.x, self.y = x, y
>>> from point import * >>> p = Point(10, 10) # __init__ 被调用 >>> type(p) <class 'point.Point'> >>> p.x, p.y (10, 10)
>>> type(Point) <class 'type'> >>> dir(Point) ['__class__', '__delattr__', '__dict__', ..., '__init__', ...] >>> Point.__class__ <class 'type'>
class Point:
...
def set(self, x, y):
self.x, self.y = x, y
>>> p = Point(10, 10) >>> p.set(0, 0) >>> p.x, p.y (0, 0)
>>> Point.set(p, 0, 0) >>> p.x, p.y (0, 0)
class Point:
...
def set(this, x, y):
this.x, this.y = x, y
class Point:
def __init__(self, x=0, y=0):
self.__x, self.__y = x, y
def set(self, x, y):
self.__x, self.__y = x, y
def __f(self):
pass
>>> p = Point(10, 10) >>> p.__x ... AttributeError: 'Point' object has no attribute '__x' >>> p.__f() ... AttributeError: 'Point' object has no attribute '__f'
>>> p = Point(10, 10) >>> p <point.Point object at 0x000000000272AA20>
>>> p Point(10, 10)
class Point:
def __repr__(self):
return 'Point({}, {})'.format(self.__x, self.__y)
class Point:
def __str__(self):
return '({}, {})'.format(self.__x, self.__y)
>>> p1 = Point(10, 10) >>> p2 = Point(10, 10) >>> p3 = p1 + p2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'Point' and 'Point'
class Point:
def __add__(self, other):
return Point(self.__x + other.__x, self.__y + other.__y)
>>> p3 = p1 + p2 >>> p3 Point(20, 20)
# shape.py
class Shape:
def area(self):
return 0.0
class Circle(Shape):
def __init__(self, r=0.0):
self.r = r
def area(self):
return math.pi * self.r * self.r
class Rectangle(Shape):
def __init__(self, a, b):
self.a, self.b = a, b
def area(self):
return self.a * self.b
>>> from shape import * >>> circle = Circle(3.0) >>> circle.area() 28.274333882308138 >>> rectangle = Rectangle(2.0, 3.0) >>> rectangle.area() 6.0
class Circle(Shape): pass
>>> Shape.area is Circle.area True
>>> from shape import * >>> Shape.area is Circle.area False
>>> Shape.__dict__['area'] <function Shape.area at 0x0000000001FDB9D8> >>> Circle.__dict__['area'] <function Circle.area at 0x0000000001FDBB70>
class Circle(Shape):
...
# def area(self):
# return math.pi * self.r * self.r
# 为 Circle 添加 area 方法。
Circle.area = lambda self: math.pi * self.r * self.r
class AbstractEventLoop:
def run_forever(self):
raise NotImplementedError
...
loop = asyncio.AbstractEventLoop() try: loop.run_forever() except NotImplementedError: pass
class AbstractShape:
def area(self):
raise NotImplementedError
class AbstractShape:
def __init__(self, color):
self.color = color
class Circle(AbstractShape):
def __init__(self, color, r=0.0):
super().__init__(color)
self.r = r
class Circle(AbstractShape):
def __init__(self, color, r=0.0):
AbstractShape.__init__(self, color)
self.r = r
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有