Python Forum
Forward __getattr__ to another object.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Forward __getattr__ to another object.
#1
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.
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.a
to return 5.

Any ideas?
Reply
#2
I dont think you need the Context class. The __getattr__ method could just return partial(self.forward, attribute)

(Aug-30-2024, 05:27 AM)deanhystad Wrote: I am trying to figure out how to do something similar with retrieving attribute values.

To do that, you need to give the proxy class a way to distinguish method names from attribute names for which you want a direct access. For example you could pass those names to the constructor
r = RemoteAlias(A(5), attrs={'a'})
then you can add a test on the attribute name in the __getattr__() method.

You could go farther in this direction by writing code to inspect the subject class to determine the method names and considering the rest as normal attribute names for example. Or you could extract the attribute names from the contents of the subject's vars() dictionary.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  what if __getattr__() can't find an attribute, either? Skaperen 1 1,990 Oct-08-2021, 08:43 PM
Last Post: Yoriz
  @property vs __set__ / __get__ and __setattr__ / __getattr__ okhajut 1 5,261 Jun-15-2021, 03:48 PM
Last Post: snippsat
  TypeError: forward() missing 1 required positional argument: 'x' sveto4ka 4 14,926 Jun-17-2020, 07:25 PM
Last Post: sveto4ka
  __getattr__ and type hint itaybardugo 0 3,309 Jul-04-2019, 09:50 PM
Last Post: itaybardugo
  Encrypt email message and forward entlearn 1 2,911 Jan-18-2019, 04:39 AM
Last Post: entlearn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020