Is it a good idea to use __class__
to create new instances within the class?
The following code is an example of doing this:
from collections import namedtuple
_Position = namedtuple('Position', ['x', 'y'])
class Position(_Position):
def __add__(self, other):
return __class__(self.x + other.x, self.y + other.y)
Using the actual class name sounds like duplicated code to me. If the name of the class changes I'd have to change it in all occurrences, even though modern IDE's can do that for you.
btw. What kind of variable is __class__
? Shouldn't you only be able to access it with self.
?