-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I don't understand this code #42
Comments
Fix: #42 In Python, truediv is used to overload the division operator (/), so when you use / between two SpecialString objects, this method is called instead of performing regular division. What does it do?: You create two objects: When you use spam / hello, this calls the truediv method and does the following: print(spam/Hello) is basically print(spam.truediv(hello)) Final Output:
|
print(spam / hello) This method is triggered when you use the / operator with instances of the SpecialString class. When spam / hello is executed, the truediv method gets called with self being spam and other being hello. It creates a line of = characters whose length is equal to the length of other.cont (which is the string in the hello object). Then, it returns a string with the following structure:
Basically in one shot you are passing two object one by self and other by parameter and then doing some concatenation and returning the result. |
来件已收到,确认后会回复给你哦
|
class SpecialString:
def init(self, cont):
self.cont = cont
spam = SpecialString("spam")
hello = SpecialString("Hello world!")
print(spam / hello)
The text was updated successfully, but these errors were encountered: