+ 1

What is (self, other) and (self, cont)? Which object is (self, other), and to which (self, cont) (spam or hello)?

Here is the code: class SpecialString: def __init__(self, cont): self.cont = cont def __truediv__(self, other): line = "=" * len(other.cont) return "\n".join([self.cont, line, other.cont]) spam = SpecialString("spam") hello = SpecialString("Hello world!") print(spam / hello) What makes the join() method? Why is there a list in the parameters of the join() method and not a simple enumeration of the parameters: self.cont, line, other.cont? Please explain in more detail.

20th Oct 2017, 6:13 AM
Norberto Spanielly
1 Answer
+ 4
The self, other, and cont are arguments to the class methods. The self is the keyword that points to the class itself, cont is just the keyword they chose for a string, and other is another string from another instance of the class. The join method joins a list, and therefore takes a list for an argument. The "\n" is what it separates the values from the list with. For example: lst = ["hello", "world"] print("\n".join(lst)) # output is: # hello # world The join method took a list as an argument, and printed the values with a line break separating them. I hope this helps, comment if you need any more clarification.
13th Nov 2017, 1:33 AM
Casey Hampson
Casey Hampson - avatar