1

我有一个make_package返回类的函数Package。在另一个文件中,我想导入 class Package,所以我可以进行类型检查。我的问题是,如何导入函数内部的类?

以下不是确切的代码,但结构相似。

# project/package.py
def make_package(ori, dest):
    class Package:
        origin = ori
        destination = dest
        def __init__(self, item):
            self.item = item
        def to_str(self):
            return self.origin + '-' + self.destination + ' : ' + self.item
    return Package

# project/shipment.py
# Don't know how to import Package from package.py
class Shipment:
    def __init__(self, **kwargs):
        self.shipment = dict()
        for container in kwargs.keys():
            self.shipment[container] = list()
            for item in kwargs[key]:
                if type(item) != Package:
                    raise TypeError('Item must be packed in Package.')
                self.shipment[container].append(item.to_str())

# project/main.py
from .package import make_package
from .shipment import Shipment
us_fr = make_package('US', 'FR')
fr_cn = make_package('FR', 'CN')
shipment = Shipment(container1=[us_fr('item1'), fr_cn('item2')], container2=[us_fr('item3'), fr_cn('item4')])
print(shipment.shipment)
# {
#     'container1' : [
#         'US-FR : item1',
#         'FR-CN' : 'item2'
#     ],
#     'container2' : [
#         'US-FR : item3',
#         'FR-CN' : 'item4'
#     ]
# }

我知道实现类型检查的一种方法是用inside创建一个虚拟变量,然后与. 然而,它似乎更像是一个黑客。我想知道有没有更好的方法?make_packageShipmenttype(item)type(dummy)

4

1 回答 1

4

没有办法从函数外部“导入”你的类,因为实际上,没有一种Package类型。每次调用时make_package(),它都会创建一个新类型,它恰好与所有其他类型具有相同的名称Package。然而,它仍然是一个独特的类型,所以它永远不会与另一个Package类型进行比较,即使origindestination是相同的。

您可以使所有Package类型都从“标记”类继承,然后用于isinstance检查项目是否为包:

# project/package.py
class PackageMarker:
    pass

def make_package(ori, dest):
    class Package(PackageMarker):
        ...  # Package body here
    return Package

# project/shipment.py
class Shipment:
    def __init__(self, **kwargs):
        self.shipment = dict()
        for container in kwargs.keys():
            self.shipment[container] = list()
            for item in kwargs[key]:
                if not isinstance(item, PackageMarker):
                    raise TypeError('Item must be packed in Package.')
                self.shipment[container].append(item.to_str())

在我看来,你用这个工厂函数创建类的方式令人困惑,但也许我只是需要更多的上下文来了解你为什么这样做。

如果您想我对如何重构它的意见,我会删除工厂函数,并执行以下操作:

# project/package.py
class Package:
    def __init__(self, route, item):
        self.route = route
        self.item = item

    def to_str(self):
        return self.route.to_str() + ' : ' + self.item

# project/route.py
class Route:
    def __init__(self, origin, destination):
        self.origin = origin
        self.destination = destination

    def to_str(self):
        return self.origin + ' - ' + self.destination

# project/shipment.py
from .package import Package

class Shipment:
    def __init__(self, **kwargs):
        self.shipment = dict()
        for container in kwargs.keys():
            self.shipment[container] = list()
            for item in kwargs[key]:
                if not isinstance(item, Package):
                    raise TypeError('Item must be packed in Package.')
                self.shipment[container].append(item.to_str())

# project/main.py
from .package import Package
from .route import Route
from .shipment import Shipment
us_fr = Route('US', 'FR')
fr_cn = Route('FR', 'CN')
shipment = Shipment(
    container1=[Package(us_fr, 'item1'), Package(fr_cn, 'item2')],
    container2=[Package(us_fr, 'item3'), Package(fr_cn, 'item4')]
)
于 2019-08-13T00:26:43.540 回答