Python类接口或继承

Python类接口或继承,python,Python,我是python新手,对接口的正确设计有点困惑。我希望一个类有不同的实现。 好吧,有两种方法可以实现这一点,但我不确定哪种方法更好: 第一种方法:在一个文件中使用类继承: 目录结构: method |--- __init__.py |--- a.py method |--- __init__.py |--- a_int.py |--- a_1.py |--- a_2.py a、 py: 用法: from method.a import A_imp1, A_imp2 instance1 = A

我是python新手,对接口的正确设计有点困惑。我希望一个类有不同的实现。 好吧,有两种方法可以实现这一点,但我不确定哪种方法更好: 第一种方法:在一个文件中使用类继承: 目录结构:

method
|--- __init__.py
|--- a.py
method
|--- __init__.py
|--- a_int.py
|--- a_1.py
|--- a_2.py
a、 py:

用法:

from method.a import A_imp1, A_imp2
instance1 = A_imp1()
instance2 = A_imp2()
instance1.do_sth()
instance2.do_sth()
from method.a_1 import A as A_1
from method.a_2 import A as A_2
instance1 = A_1.A()
instance2 = A_2.A()
instance1.do_sth()
instance2.do_sth()

第二种方法: 目录结构:

method
|--- __init__.py
|--- a.py
method
|--- __init__.py
|--- a_int.py
|--- a_1.py
|--- a_2.py
a_int.py:

class A_interface(object):
    def do_sth(self):   pass
a_1.py:

from a_int import A_interface
class A(A_interface):
        def do_sth(self): return 1
a_2.py

from a_int import A_interface
class A(A_interface):
    def do_sth(self): return 2
用法:

from method.a import A_imp1, A_imp2
instance1 = A_imp1()
instance2 = A_imp2()
instance1.do_sth()
instance2.do_sth()
from method.a_1 import A as A_1
from method.a_2 import A as A_2
instance1 = A_1.A()
instance2 = A_2.A()
instance1.do_sth()
instance2.do_sth()

哪一种(如果有的话)是实现这一目标的正确的pythonic方法?

取决于实际情况,两者都是有效的

假设你为员工写课程,也许你从

class Person(object):
    def __init__(self, name):
        self._name = name

    def get_name(self):
        return self._name

    def get_position(self):
        return 'Not Hired'

class Employee(Person):
    def get_position(self):
        return 'Employee'

class Manager(Employee):
    def __init__(self, *args, **kwargs):
        self.employees = set()
        super(Manager, self).__init__(*args, **kwargs)

    def get_position(self):
        return 'Manager'

    def manage(self, employee):
        self.employees.add(employee)
员工仍然可以访问其
get\u name
方法,但它将覆盖
get\u position
方法。对经理来说也是一样

现在,假设您正在编写服务器

class BaseServer(object):
    def handle_request(self, request):
        raise NotImplementedError

class Server(BaseServer):
    def handle_request(self, request):
        ... do stuff ...
在这里,您有一个接口,因为您希望确保任何子类都会覆盖您的
handle\u请求
方法


根据您正在做的事情,任何一种方法都可以工作。

实现一个(跳到第4节:面向对象语言中的软件接口)的正确方法是使用一个抽象基类,而不是一个简单的基类,这样所有从它继承的类都可以实现它们自己的方法:

from abc import ABCMeta, abstractmethod

class A(object):
    __metaclass__ = metaclass=ABCMeta

    @abstractmethod
    def do_sth(self):   pass

class A_imp1(A):
    def do_sth(self): return 1

class A_imp2(A):
    def do_sth(self): return 2
现在,用户无法自己初始化
a
,并且从
a
继承的任何类,如果没有定义自己的
操作,也将无法初始化:

>>> A()

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    A()
TypeError: Can't instantiate abstract class A with abstract methods do_sth


>>> class A_imp3(A):
    pass

>>> a = A_imp3()

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    a = A_imp3()
TypeError: Can't instantiate abstract class A_imp3 with abstract methods do_sth
>>A()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
()
TypeError:无法用抽象方法实例化抽象类A
>>>A级\u imp3(A):
通过
>>>a=a_imp3()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
a=a_imp3()
TypeError:无法用抽象方法实例化抽象类A\u imp3

话虽如此,我认为你的两种方法在我看来都不错。这基本上就是你想要如何管理你的课程。Python 3中的所有内容都在一个地方定义。

我偶然发现了这一点,我需要感谢您提供的服务器示例,因为这是迄今为止我所看到的抽象类用例的完美示例。