在类中生成动态方法

发布于 2024-12-23 08:30:02 字数 609 浏览 1 评论 0原文

我想要一个具有动态方法的类。

从这些数组中:

prefix = ['android','iphone','blackberry'] 
method = ['AddToPush','DelFromPush','GetPushMessages']

我想创建一个类,例如:

class MobileManager(object)

    def __init__(self):
        self.mobileLib = MobileLib()

    def androidAddToPush(self, args):
        self.mobileLib.AddToPush(args, 'android')

    def iphoneAddToPush(self, args):
        self.mobileLib.AddToPush(args, 'iphone')    

    def blackberryAddToPush(self, args):
        self.mobileLib.AddToPush(args, 'blackberry') 

   [...]

如何在运行时生成/创建这些方法?

I want to have a class with dynamic methods.

From these arrays:

prefix = ['android','iphone','blackberry'] 
method = ['AddToPush','DelFromPush','GetPushMessages']

I want to create a class like:

class MobileManager(object)

    def __init__(self):
        self.mobileLib = MobileLib()

    def androidAddToPush(self, args):
        self.mobileLib.AddToPush(args, 'android')

    def iphoneAddToPush(self, args):
        self.mobileLib.AddToPush(args, 'iphone')    

    def blackberryAddToPush(self, args):
        self.mobileLib.AddToPush(args, 'blackberry') 

   [...]

How can I have these methods generated/created at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-12-30 08:30:02

如果你的前缀/方法集是在初始化时定义的,你可以尝试这样的事情:

class MobileManager(object):
    def __init__(self):
        for prefix_name in prefix:
            for method_name in method:
                func = lambda self, args: getattr(self.mobileLib, method_name)(args, prefix)
                full_method_name = "%s%s" % (prefix, method_name)
                setattr(self, full_method_name, func)
        ...

如果你的动态方法变得更加复杂,那么 lambda 很快就会受到限制。

If your set of prefixes/methods is defined at init time, you can try something like this:

class MobileManager(object):
    def __init__(self):
        for prefix_name in prefix:
            for method_name in method:
                func = lambda self, args: getattr(self.mobileLib, method_name)(args, prefix)
                full_method_name = "%s%s" % (prefix, method_name)
                setattr(self, full_method_name, func)
        ...

If your dynamic methods get more complicated, the lambda will soon be limited, though.

浅听莫相离 2024-12-30 08:30:02

或者为什么不继承

class MobileManager(object)

    def __init__(self):
        self.mobileLib = MobileLib()

    def AddToPush(self, args, platform=None):
        self.mobileLib.AddToPush(args, platform)

class Android(MobileManager):
    def __init__(self):
        MobileManager.__init__(self)

    def AddToPush(self, args):
         MobileManager.AddToPush(args, platform="android")

Or why not inherate

class MobileManager(object)

    def __init__(self):
        self.mobileLib = MobileLib()

    def AddToPush(self, args, platform=None):
        self.mobileLib.AddToPush(args, platform)

class Android(MobileManager):
    def __init__(self):
        MobileManager.__init__(self)

    def AddToPush(self, args):
         MobileManager.AddToPush(args, platform="android")
脸赞 2024-12-30 08:30:02

动态方法通常是错误的方法,会导致代码混乱。

在这里,我会这样做:

class Mobile(object):
    def add_to_push(self, args):
        ....
    def del_from_push(self, args):
        ...
    def get_push_methods(self, args):
        ...

在您的管理器中:

class MobileManager(object):
    def __init__(self):
        self.android = Mobile()
        self.blackberry = Mobile()
        self.iphone = Mobile()

现在,您可以编写 manager.android.add_to_push(args),而不是 manager.mobileLib.add_to_push(args, 'android')

如果您有平台变量,您甚至可以动态调度:getattr(manager, platform).add_to_push(args)

如果您希望 3 种类型的平台有不同的行为,您可以创建 Mobile 的子类(可能是 AndroidMobile、BlackberryMobile 和 iPhoneMobile)。

当一个新平台出现时(也许是windows7),如何更改代码来支持它是显而易见的。

Dynamic methods are often the wrong approach leading to confusing code.

Here, I'd do this:

class Mobile(object):
    def add_to_push(self, args):
        ....
    def del_from_push(self, args):
        ...
    def get_push_methods(self, args):
        ...

And in your Manager:

class MobileManager(object):
    def __init__(self):
        self.android = Mobile()
        self.blackberry = Mobile()
        self.iphone = Mobile()

Now, instead of manager.mobileLib.add_to_push(args, 'android'), you would write manager.android.add_to_push(args).

You can even dispatch dynamically if you have a platform variable: getattr(manager, platform).add_to_push(args).

If you want different behaviour for the 3 types of platform you can make subclasses of Mobile (perhaps AndroidMobile, BlackberryMobile and IPhoneMobile).

When a new platform appears (perhaps windows7) it's obvious how to change the code to support it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文