This is my first post. Should have come earlier. Too much work on the other courses. Sorry about that. Won't happen again.
Anyway, python is new to me since I didn't take CSC108. But the course materials are pretty much covered in the ECE244 course I took last term, which is c++. Although the syntax and different type of variables kept bugging me since the start of this course, I found it really easy to program in python. I think the reason is that most programming languages, rules and logic are almost the same.
Since I learnt class in c++, I will talk about the differences between classes in python and c++.
1) The __init__ method is like the constructor which initializes the class when an object is created. It can take only self argument (which in c++ is void) or several arguments. But class in c++ can have more than one constructor and the default constructor is provided if you don't write one. I am not sure about the class in python.
2) The __str__ methods from both languages basically are the same. print(class) = cout << class. The one from python returns the string form of the object and in c++ you write an overloaded operator<< to print an object.
3) The __eq__ methods are the same. In c++ there is an method "operator==" corresponds to this. And it should perform the deep compare.
4) An another major difference is that there is no so-called private member in class of python. A private member can essentially only be altered by a member method which is called mutator. And functions that can access the member variables or methods are called accessor.
5) One thing I am not sure about is that the unique "self" in python. In c++, there always exists a pointer "this" that points to the the current object. And when you write "member_variable = blahblahblah", what actually happened is "(*this).member_variable = blahblahblah" (* is dereference) and usually we omit the (*this). Does the "self" in python function the same way as (*this)?
4) An another major difference is that there is no so-called private member in class of python. A private member can essentially only be altered by a member method which is called mutator. And functions that can access the member variables or methods are called accessor.
5) One thing I am not sure about is that the unique "self" in python. In c++, there always exists a pointer "this" that points to the the current object. And when you write "member_variable = blahblahblah", what actually happened is "(*this).member_variable = blahblahblah" (* is dereference) and usually we omit the (*this). Does the "self" in python function the same way as (*this)?
No comments:
Post a Comment