1901030012-MIT-Python课-lecture 8

学员信息

  • 学号:1901030012
  • 学习内容:MIT第8课视频文稿p7-8及幻灯片p12-13
  • 学习用时:67min Notes no.: Day37

学习笔记

  1. assigned vs. equal to

    So now we have sort of a nice class. It’s very simple, but we can start actually creating coordinate objects. So when you create coordinate objects, you’re creating instances of the class.

    So this line here, C is equal to coordinate 3,4, is going to call the init method. It’s going to call the init method with x is equal to 3 and y is equal to 4.

Cat notes:
Here when prof. “read” this piece of codes, “=” read as “equal to” however be aware of its actual specific meaning in Python, “=” means to assign or passing it all the variables, “==” means to check operational “equal to”, and also bear in mind about “!=” —not equal to, when using in arguments to demonstrate boolean operation.

  1. dot notion
    We can access the data attributes using this dot notation and we’ve seen that before, right?
    When we’ve worked with lists we’d say something like, L dot append, right, when we create a list. So the same dot notation can be used with your own objects in order to access data attributes. So here, this is going to print 3 because the x value for object C is 3, and the next line, print origin x is going to print 0 because the x value for the object origin is 0.

  2. init method
    We have to find the init method so we have a way to create objects when we use the class. And then we can access the data attributes. But that’s kind of lame, right, because there isn’t anything cool we can do with it.

  3. add more methods
    Remember methods are going to be procedural attributes that allow us to interact with our object.

Methods are like functions except that there’s a couple of differences which you’ll see in a moment.

eg: “distance”
So notice this method is pretty much like a function, right? You have DF, some name, it takes in parameters. It does some stuff and then it returns a value. The only difference is the fact that you have a self here as the first thing and the fact that you always have to be conscious about whose data attributes you’re accessing. So you have to use the dot notation in order to decide whose data attributes you want access. So we’ve defined the method here, distance.

  1. Euclidean distance formula
    So here I’m going to just implement the Euclidean distance formula, which is x1 minus x2 squared, plus y1 minus y2 squared, and square root of all that. So that’s what I’m doing inside here.
1
2
3
4
5
6
7
8
class Coordinate(object):
def __init__(self, x, y):
self.x = x
self.y = y
def distance(self, other):
x_diff_sq = (self.x - other.x) ** 2
y_diff_sq = (self.y - other.y) ** 2
return (x_diff_sq + y_diff_sq) ** 0.5

Other than self and dot notation, methods behave like functions (take params, do operations, return)

  1. 我看到的
    时间和实践是最好的朋友,当和最好的朋友在一起的时候,人是真诚、充实、无需谎言的。
    我们的Camp给我们提供了默认的methods来让我们和自己最好的朋友尽可能的相处,我们也许还不知道default的缘由,但是,能使用到这些methods然后逐渐地通过一步一步的迈进能看到default以及缘由,我们自己就可以create我们的methods,与别人交流,供别人使用,我觉得我这次真的看到了希望。下一步是,build it up。
    感谢营友们的美好,这个世界有你们真好!!!
    加油!