Dynamically choosing class to inherit from

2019-03-14 07:15发布

My Python knowledge is limited, I need some help on the following situation.

Assume that I have two classes A and B, is it possible to do something like the following (conceptually) in Python:

import os
if os.name == 'nt':
    class newClass(A):
       # class body
else:
   class newClass(B):
       # class body

So the problem is that I would like to create a class newClass such that it will inherit from different base classes based on platform difference, is this possible to do in Python? Thanks.

5条回答
劳资没心,怎么记你
2楼-- · 2019-03-14 07:32

I have found my own way of dynamically inheriting classes when you have more than two options to chose from. I'm sure that others have used this before me, however I couldn't find anything like this online, therefore I thought that I should share it with you.

class A():
    # Class A Body
class B():
    # Class B Body
class C():
    # Class C Body
class D():
    # Class D Body
def MakeObject(InheritedClassName): # InheritedClassName is a string
    def CheckClass(InheritedClass):
        if InheritedClass == "A":
            return A
        elif InheritedClass == "B":
            return B
        elif InheritedClass == "C":
            return C
        else:
            return D
    class WantedClass(CheckClass(InheritedClassName)):
        # WantedClass Body
    YourObject = WantedClass()
    return YourObject
CreateObject = MakeObject(str(input("Chose A, B, C or D")))

This code can be altered and you can do quite allot with it. This is my favorite way of setting up dynamic class inheritance, even though it's a bit long, as you can have as many options as you want and even could have multiple dynamically chosen inheritances for each object.

查看更多
Bombasti
3楼-- · 2019-03-14 07:36

You can use a conditional expression:

class newClass(A if os.name == 'nt' else B):
    ...
查看更多
\"骚年 ilove
4楼-- · 2019-03-14 07:43

Yep, you can do exactly what you wrote. Though personally, I'd probably do it this way for cleanliness:

class _newClassNT(A):
    # class body

class _newClassOther(B):
    # class body

newClass = _newClassNT if os.name == 'nt' else _newClassOther

This assumes that you need to actually do different things implementation-wise within the class body. If you only need to change the inheritance, you can just embed an if statement right there:

class newClass(A if os.name == 'nt' else B):
    # class body
查看更多
一纸荒年 Trace。
5楼-- · 2019-03-14 07:50

Coming from the worlds of Java and C#, the thought of doing something like this makes me cringe =P. (Not that the idea of conditionally inheriting a class is bad - I'm just not used to it.)

Thinking about it for a bit, I would have done something like this if this question were about Java. I'm posting the pattern - implement an interface, then use a factory to select between the implementations - in order to provide another perspective to the problem:

public interface OsDependent {
  public void doOsDependentStuff();
}

public class WindowsDependentComponent implements OsDependent {
  @Override
  public void doOsDependentStuff() {
    //snip
  }
}

public class AppleDependentComponent implements OsDependent {
  @Override
  public void doOsDependentStuff() {
    //snip
  }
}

public class OsDependentComponentFactory {
  public OsDependent getOsDependentComponent(Platform platform) {
    if(platform == Platform.WINDOWS)
      return new WindowsDependentComponent();
    else if(platform == Platform.APPLE)
      return new AppleDependentComponent();
    else
      return null;
  }
}

Definitely a lot more code, but it's an appropriate solution in a strongly typed environment.


EDIT: One significant difference I noticed between my answer and the original question:

If you conditionally inherit from multiple different classes, then the superclasses contain code that depend on which OS you're using, while the class that inherits from them contains code that is the same for all OS's. The top of the inheritance chain is OS-dependent; the bottom isn't.

My approach goes the other way. The OsDepndent interface (or superclass) defines methods that are similar for all platforms, while the different implementations (or subclasses) have OS-dependent code. The top of the inheritance chain is OS-agnostic.

查看更多
Ridiculous、
6楼-- · 2019-03-14 07:53

For those who have similar needs whilst working with different modules. First, create a fileswitcher.py and put inside it the following

class Switcher():
    def choose(clname):
         Switcher.clname = clname

Next, in your newClass.py put

import switcher

class newClass(switcher.Switcher.clname):
    ...

Finally in your main script

import switcher
import os
    if os.name == 'nt':
        switcher.Switcher.choose(A):
    else:
        switcher.Switcher.choose(B):

# that's the moment you're creating class with desired inheritance
import newClass.newClass
查看更多
登录 后发表回答