After another day in the water, I feel that I forget what I have learned quickly. I’ve forgotten the basics of Python the other day.
Constructor review
The constructor
It’s a function that’s automatically called when you create an object, and not defining a constructor doesn’t mean that there’s no constructor, it’s a default constructor that does nothing, right
Overloaded constructor
Like normal functions, constructors can be overloaded
Function overloading is not allowed in C language, and many times it will lead to a lot of unnecessary trouble, such as swap function, which needs to exchange two variables of various types. At this time, function overloading in C++ can be used to achieve ‘one function’ to achieve multiple functions. Function overloading: different function lists (parameter number, parameter type, parameter order) can share the same function name.
Copy constructor
In the case of class objects, class objects of the same type are copied through the copy constructor.
person A;
person B=A;// Call the copy constructor
Copy the code
The Format method of strings in Python
print('{1} {2} {0}'.format("chen".'hello! '.'hesor'))
print('{1}{0:.2f}'.format(3.1415.'PI='))
print('%c=%.2f' % (97.3.14))
print('%-5d#%d' % (35.53))
print('%5d#%d' % (35.53))
''' Output: hello! Hesorchen PI= 3.14a = 3.1435 #53
Copy the code