For the code below:
class B(A):
def __init__(self):
self.a = 4
def set_a(self, value):
self.a = value
Match the names B
, A
, a
, set_a
to the roles below:
The following class could be used to represent a point in 2-D space:
class Point:
""" Point class for representing and manipulating x,y coordinates """
def __init__(self, x, y):
""" Create a new point at the given coordinates. """
self.x = x
self.y = y
def get_x(self):
return self.x
def get_y(self):
return self.y
def distance_from_origin(self):
return ((self.x ** 2) + (self.y ** 2)) ** 0.5
Assume we executed p = Point(4, 5)
, which of the following would modify the x
instance variable?
p.x + 2
p.x += 2
p.x = 3
p.get_x() + 2
p.get_x() += 2
p.get_x() = 3
Using the code above, Point(4, 5) == Point(4, 5)
evaluates to False
. Why is that? How could you extend the Point
class so that above expression returns True
when the two points have the same coordinates?
Implement a distance
method on Point
that returns the distance between the receiver point (the point object on which the method is invoked and another point object).
If you were going to create a new class Point3D
for 3-D points (i.e., with x, y and z coordinates) that derived from Point
, which of the following methods would you need to override in the Point3D
class?
__init__
get_x
get_y
distance_from_origin
Implement the initializer method for the Point3D
class described above.
What is printed as this code executes?
class Foo():
def __init__(self, a):
self.a = a
self.b = 2
class Bar(Foo):
def __init__(self, c):
super().__init__(4)
self.c = c
bar = Bar(6)
print(bar.a, bar.b, bar.c)