I have a class like so:
class NumberGenerator(object):
def __init__(self, seed=0):
random.seed(seed)
self.current = random.random()
def __next__(self):
self.next()
def next(self):
self.current = random.random()
return self.current
Ideally, I would like to do the following via some magic method if available.
>>> numGen = NumberGenerator(99)
>>> numGen.next()
0.15
>>> numGen + 2
2.15
I know __str__
and __repr__
and __add__
etc. But what I want is a magic method to return something whenever the object is referenced other than a pointer to the object. I will not assign another variable to the object reference, downstream, I promise. Is this available in Python?
some_var
is always a reference to an object. – juanpa.arrivillaga+
operator. It's__add__
that you must override. – Pedro von Hertwig Batista