0

I would like to have an object that stores an internal function that is evaluate on each read access, so the value returned for those read access is somehow dynamic.

Example :

import time


class MyWrapper:
    def __init__(self, function):
        self.function = function
    
    # Example 
    def __read_access__(self):
        return self.function()


def test_function():
    return (time.time() % 2 ) > 0


test = MyWrapper(test_function)

# Tested multiple times because it's kind of random
print(f"{test=}")
print(f"{test=}")
print(f"{test=}")

# Should be working with 'is' comparator with True, False and None
print(f"{test is True}")
print(f"{test is True}")
print(f"{test is True}")

Example results:

test=True
test=False
test=True

False
False
True

For instance it can be used to bound a variable to the content of a file, or even a database row, in a transparent way for the user of the variable.

The @property works pretty well but, for what I've understood, it's only for a class method with a instance.value access, but can't be used directly on a variable.

Olivier
  • 86
  • 6
  • 1
    No, unfortunately, this isn't going to work as you want it. There's no special method that gets called when a local variable is accessed, only when a variable on an instance of a class gets accessed via dot notation (`self.test`). If that works for you, you can use `@property`, but unfortunately you can't do it for locals. – Silvio Mayolo Aug 27 '23 at 23:44
  • You can implement various special methods that will get invoked in various scenarios, like `__str__` or `__repr__` in your first case. But there's no overloading for the `is` operator, and no generic "any access" method, as that is very undefined. would `foo(test)` "access" the variable? – deceze Aug 27 '23 at 23:49
  • Why do you want to do this? It’s probably not the best way to achieve the underlying goal. (Not to mention that that `is` behaviour is impossible within Python.) – Ry- Aug 27 '23 at 23:58
  • Does this help? Your question doesn't nail down any specific behavior so it's hard to tell. https://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters – Kenny Ostrom Aug 28 '23 at 01:55

1 Answers1

-3

The is operator in Python is used to test if two variables/objects refer to the same object in memory or not. It compares the identity of two objects, rather than their values

it is much like saying two lines below are the same

obj1 is obj2
id(obj1) == id(obj2)

so no work around for that case

for the implementation I would do something like this:

import time


class MyWrapper:
    def __init__(self, function):
        self.function = function
    
    @property
    def get_function(self):
        return self.function()

    def __str__(self):
        raise Exception("Cannot Access object directly")

    def __repr__(self) -> str:
        raise Exception("Cannot Access object directly")
  
    def __eq__(self, __value: object) -> bool:
        raise Exception("Cannot Access object directly")
    



def test_function():
    return (time.time() % 2 ) > 0


test = MyWrapper(test_function)

print(f"{test.get_function=}")
print(f"{test.get_function=}")
print(f"{test.get_function=}")
print(f"{test is True}")
print(f"{test is True}")
print(f"{test is True}")

force user to access class via property and raise exception otherwise