Do you want BuboFlash to help you learning these things? Or do you want to add or correct something? Click here to log in or create user.



Tags
#python
Question

The expression x + y is translated into x.__add__(y).

However, if x hasn't implemented __add__, and x and y are of different types, then y.__radd__(x) is called.
There are equivalent r methods for all magic methods just mentioned.
Example:
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])


[...]
hello = SpecialString("Hello world!")
print(spam / hello)Try It Yourself
Result:>>>
spam
============
Hello world!
>>>
In the example above, we defined the division operation for our class SpecialString.
Answer
spam = SpecialString("spam")

Tags
#python
Question

The expression x + y is translated into x.__add__(y).

However, if x hasn't implemented __add__, and x and y are of different types, then y.__radd__(x) is called.
There are equivalent r methods for all magic methods just mentioned.
Example:
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])


[...]
hello = SpecialString("Hello world!")
print(spam / hello)Try It Yourself
Result:>>>
spam
============
Hello world!
>>>
In the example above, we defined the division operation for our class SpecialString.
Answer
?

Tags
#python
Question

The expression x + y is translated into x.__add__(y).

However, if x hasn't implemented __add__, and x and y are of different types, then y.__radd__(x) is called.
There are equivalent r methods for all magic methods just mentioned.
Example:
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])


[...]
hello = SpecialString("Hello world!")
print(spam / hello)Try It Yourself
Result:>>>
spam
============
Hello world!
>>>
In the example above, we defined the division operation for our class SpecialString.
Answer
spam = SpecialString("spam")
If you want to change selection, open document below and click on "Move attachment"

Magic Methods
st mentioned. Example: 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]) <span>spam = SpecialString("spam") hello = SpecialString("Hello world!") print(spam / hello)Try It Yourself Result:>>> spam ============ Hello world! >>> In the example above, we defined the division op

Summary

statusnot learnedmeasured difficulty37% [default]last interval [days]               
repetition number in this series0memorised on               scheduled repetition               
scheduled repetition interval               last repetition or drill

Details

No repetitions


Discussion

Do you want to join discussion? Click here to log in or create user.