0
votes

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?

1
"But what I want is a magic method to return something whenever the object is referenced other than a pointer to the object." No pointers are returned. Python doesn't have pointers. Fundamentally, you cannot do this. some_var is always a reference to an object.juanpa.arrivillaga
For your purpose, you might be able to get away with overloading the + operator. It's __add__ that you must override.Pedro von Hertwig Batista
@juanpa.arrivillaga Okay, and how is that reference implemented in Python, if it's not a pointer to the object?neo4k
@neo4k the implementation details are irrelevant, there are no pointers in the Python. It is best to stick to the semantics of the language, unless you are actually interested in the implementation details of a particular implementation.juanpa.arrivillaga
@neo4k Note the phrase "under the hood". The statement is simply saying that the C code that implements Python uses pointers. This doesn't mean the Python language itself has pointers, only that the language Python is implemented in - C - has pointers.Christian Dean

1 Answers

3
votes

The Python language doesn't include pointers. You can think of variables in Python as references to some object. When using a variable, you're accessing the object the variable is referring to.

The behavior you want your NumberGenerator class to have, can be implemented by implementing the __add__ magic method. This is the method Python calls when the + operator is being used with an instance of your class on the left side of the expression. In other words:

numGen + 2

Is translated to:

numGen.__add__(2) 

Note that if you want to use the + operator regardless of which side of the expression your class instance is on, you need to also implement the __radd__ magic method:

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

    def __add__(self, value):
        return self.current + value

    def __radd__(self, value):
        return self.__add__(value)

Here is an example of the class being used:

>>> numGen = NumberGenerator(99)
>>> numGen.next()
0.20007544457494542
>>> numGen + 2
2.2000754445749453
>>> 2 + numGen
2.2000754445749453
>>>