REMEMBER PYTHON

MINTE9
2REMEMBER




Attributes

p265 ! You can assign values to an instance using dot.
 RunCode
class Point:
    """ a 2D point """

p = Point()
p.x = 1
p.y = 2

print(p.x) # 1
print(p.y) # 2
... 7 lines
˄˄˄
You can pass an instance as an argument.
 RunCode
class Point:
    """ a 2D point """

p = Point()
p.x = 1
p.y = 2

def print_point(point):
    print('(%s, %s)' % (point.x, point.y))

print_point(p) # (1, 2)
... 8 lines
˄˄˄
! The docstring lists the attributes.
 RunCode
class Rectangle:
    """Represents a rectangle.

    attributes: width, height, corner
    """
    
class Point:
    """A 2-D point"""
    # simple comment (not used by __doc__)

box = Rectangle()
box.width = 100
box.height = 200

# embeded object
box.corner = Point()
box.corner.x = 0
box.corner.y = 0

print(box.width) # 100
print(Rectangle.__doc__) # Represents a rectangle ...
print(Point.__doc__) # A 2-D point
... 17 lines
˄˄˄
Task

 CompleteTask
"""
Write a function named distance_between_points()
that takes 2 Point as argument
and return the distance between them """

import math

class Point: """ a 2D point """

a = Point()
a.x = 0.0
a.y = 0.0

b = Point()
b.x = 3.0
b.y = 4.0

# SOLUTION
    ???
solution code
Questions    
No. 26   Functional Programming
CARD
25
32

        A B C D E F
🔔
1/2