如何根据参数向类添加函数

2024-06-11 14:07:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个类(node),我想根据类(operatingMode)的一个参数为它分配一组特定的函数(在本例中是a1a2b1b2)。你知道吗

情况是,我有一个电机,有许多不同的工作模式。每个操作模式允许执行某些功能,但不允许执行其他功能。将函数分配给各种模式的方式并不适合为每种操作模式创建类。你知道吗

下面是我的通行证在一个解决方案,但它不工作。你知道吗

有什么想法吗?你知道吗

def a1(self):
    return 'a1'

def a2(self):
    return 'a2'

def b1(self):
    return 'b1'

def b2(self):
    return b2




class node(object):
    def __init__(self,operatingMode):
    self.operatingMode=operatingMode

    if self.operatingMode=='A':
        self.a1function=a1
        self.a2function=a2
        print 'Operating Mode \'A\' functions loaded'

    if self.operatingMode=='B':
        self.b1function=b1
        self.b2function=b2
        print 'Operating Mode \'B\' functions loaded'

    def setOperatingMode(self,operatingMode):
        self.operatingMode=operatingMode
        self.__init__(self,operatingMode)

在我的终端上运行这个可以调用它,但是我必须声明两次:

In [65]: elbow=node('A')
Operating Mode 'A' functions loaded

In [66]: elbow.a1function(elbow)
Out[66]: 'a1'

尝试运行elbow.setOperatingMode('B')会产生错误。你知道吗


Tags: selfnodea2returnmodedefa1模式
2条回答

也许使用checkMode装饰器会是一种更干净的方法--这避免了__getattr__type.MethodType魔法:

def checkMode(mode):
    def deco(func):
        def wrapper(self):
            if self.operatingMode == mode:
                return func(self)
            else:
                raise TypeError('Wrong operating Mode')
        return wrapper
    return deco

class Node(object):
    def __init__(self, operatingMode):
        self.operatingMode = operatingMode

    @checkMode('A')
    def a1(self):
        return 'a1'

    @checkMode('A')
    def a2(self):
        return 'a2'

    @checkMode('B')
    def b1(self):
        return 'b1'

    @checkMode('B')
    def b2(self):
        return 'b2'

通过上面的代码,我们可以做到:

elbow = Node('A')
print(elbow.a1())
# a1

knee = Node('A')
print(knee.a1())
# a1

elbow.operatingMode = 'B'
print(knee.a1())  # knee is still in operatingMode A
# a1

print(elbow.b2())
# b2

elbow.a1()
# TypeError: Wrong operating Mode

说明:

decorator语法的工作方式如下:

@deco
def func(): ...

相当于

def func(): ...
func = deco(func)

在上面,checkMode是一个返回装饰器deco的函数。 deco然后装饰方法a1a2等,以便

a1 = deco(a1)

因此,a1是传递给deco的func。deco(a1)依次返回一个新方法,一般称为wrapper。这个新方法通过语句a1 = deco(a1)分配给a1。所以a1现在是方法wrapper。因此,当您调用elbow.a1()时,wrapper中的代码将被执行。你知道吗

使用types.MethodType__getattr__

import types

def a1(self):
    return 'a1'

def a2(self):
    return 'a2'

def b1(self):
    return 'b1'

def b2(self):
    return 'b2'

class Node(object):
    def __init__(self, operatingMode):
        self.operatingMode = operatingMode
    def __getattr__(self, attr):
        if self.operatingMode=='A':
            if attr == 'a1function':
                return types.MethodType(a1, self)
            if attr == 'a2function':
                return types.MethodType(a2, self)

        elif self.operatingMode=='B':
            if attr == 'b1function':
                return types.MethodType(b1, self)
            if attr == 'b2function':
                return types.MethodType(b2, self)
        else:
            raise AttributeError()

那么

elbow = Node('A')
print(elbow.a1function())
elbow.operatingMode = 'B'
print(elbow.b2function())

收益率

a1
b2

相关问题 更多 >