当将方法作为类外的对象传递时,是否可以保持方法的“边界”(Is it possible to maintain “boundness” of a method when passing it as a

编程入门 行业动态 更新时间:2024-10-27 06:18:45
当将方法作为类外的对象传递时,是否可以保持方法的“边界”(Is it possible to maintain “boundness” of a method when passing it as an object outside its class)

我正在尝试编写一个库,它将从多个服务端点向容器注册任意服务调用列表。 我打算在每个服务编写一个类中实现服务调用。 有没有办法在将服务类注册到容器时保持方法的有界性(因此它们仍然可以访问它们自己的对象实例的实例数据),或者我必须注册整个对象然后写一些排序使用__getattr__或其他类似方法在容器类中传递以访问实例上下文中的方法?

容器:

class ServiceCalls(object): def __init__(self): self._service_calls = {} def register_call(self, name, call): if name not in self._service_calls: self._service_calls[name] = call def __getattr__(self, name): if name in self._service_calls: return self._service_calls[name]

服务:

class FooSvc(object): def __init__(self, endpoint): self.endpoint = endpoint def fooize(self, *args, **kwargs): #call fooize service call with args/kwargs utilizing self.endpoint def fooify(self, *args, **kwargs): #call fooify service call with args/kwargs utilizing self.endpoint class BarSvc(object): def __init__(self, endpoint): self.endpoint = endpoint def barize(self, *args, **kwargs): #call barize service call with args/kwargs utilizing self.endpoint def barify(self, *args, **kwargs): #call barify service call with args/kwargs utilizing self.endpoint

实施代码:

foosvc = FooSvc('fooendpoint') barsvc = BarSvc('barendpoint') calls = ServiceCalls() calls.register('fooize', foosvc.fooize) calls.register('fooify', foosvc.fooify) calls.register('barize', barsvc.barize) calls.register('barify', barsvc.barify) calls.fooize(args)

I'm trying to write a library that will register an arbitrary list of service calls from multiple service endpoints to a container. I intend to implement the service calls in classes written one per service. Is there a way to maintain the boundedness of the methods from the service classes when registering them to the container (so they will still have access to the instance data of their owning object instance), or must I register the whole object then write some sort of pass through in the container class with __getattr__ or some such to access the methods within instance context?

container:

class ServiceCalls(object): def __init__(self): self._service_calls = {} def register_call(self, name, call): if name not in self._service_calls: self._service_calls[name] = call def __getattr__(self, name): if name in self._service_calls: return self._service_calls[name]

services:

class FooSvc(object): def __init__(self, endpoint): self.endpoint = endpoint def fooize(self, *args, **kwargs): #call fooize service call with args/kwargs utilizing self.endpoint def fooify(self, *args, **kwargs): #call fooify service call with args/kwargs utilizing self.endpoint class BarSvc(object): def __init__(self, endpoint): self.endpoint = endpoint def barize(self, *args, **kwargs): #call barize service call with args/kwargs utilizing self.endpoint def barify(self, *args, **kwargs): #call barify service call with args/kwargs utilizing self.endpoint

implementation code:

foosvc = FooSvc('fooendpoint') barsvc = BarSvc('barendpoint') calls = ServiceCalls() calls.register('fooize', foosvc.fooize) calls.register('fooify', foosvc.fooify) calls.register('barize', barsvc.barize) calls.register('barify', barsvc.barify) calls.fooize(args)

最满意答案

您正在尝试做的工作正常,正如您可以通过运行自己的代码看到的那样。 :)

对象foosvc.fooize在Python中称为“绑定方法”,它包含对foosvc和函数FooSvc.fooize 。 如果调用bound方法,则对self的引用将作为第一个参数隐式传递。

另外,对于无效的属性名称, __getattr__()不应以静默方式返回None 。 更好地利用这个:

def __getattr__(self, name): try: return self._service_calls[name] except KeyError: raise AttributeError

What you are trying to do will work fine, as you can see by running your own code. :)

The object foosvc.fooize is called a "bound method" in Python, and it contains both, a reference to foosvc and to the function FooSvc.fooize. If you call the bound method, the reference to self will be passed implicitly as the first paramater.

On a side note, __getattr__() shouldn't silently return None for invalid attribute names. Better use this:

def __getattr__(self, name): try: return self._service_calls[name] except KeyError: raise AttributeError

更多推荐

本文发布于:2023-07-20 11:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   边界   对象   maintain   boundness

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!