Aug-30-2024, 05:27 AM
I have a class, RemoteAliast that is a local representative for a python object that exists elsewhere. Calling a method on the alias results in the method getting called on the remote object, and the value returned.
I am trying to figure out how to do something similar with retrieving attribute values. I want calling this:
Any ideas?
class RemoteAlias: """Local standin for an object that resides elsewhere.""" def __init__(self, remote): self.remote = remote def __getattr__(self, attribute): return Context(self, attribute) def forward(self, attribute, *args): return getattr(self.remote, attribute)(*args) class Context: """Encapsulation of a method call.""" def __init__(self, remote, attribute): self.remote = remote self.attribute = attribute def __call__(self, *args): return self.remote.forward(self.attribute, *args) class A: def __init__(self, a): self.a = a def mult(self, b): return self.a * b r = RemoteAlias(A(5)) print(r.mult(2))
Output:10
Context.__call__() makes sure the method call is passed along to RemoteALias.forward. In the real code "forward()" sends a message over a socket connection to a server where the remote object resides.I am trying to figure out how to do something similar with retrieving attribute values. I want calling this:
r.ato return 5.
Any ideas?