Python中的对象类型转换(设计建议) [英] Object type casting in Python (design suggestion)

查看:173
本文介绍了Python中的对象类型转换(设计建议)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们假设有一个函数get_pack(),它返回一个Pack对象:

  
def __init __(self,name,weight):
self.name = name
self.weight = weight
...



我想要做的是将此对象包装到我的对象中,这就是说,CommercialPack有一些有用的函数:

  class CommercialPack(Pack):
def get_delivery_price(self,distance):
return self.weight * PER_KM_PRICE * distance
...

然后创建一个返回CommercialPack而不是Pack的函数。在其他语言(如Delphi或Java)中,您可以在运行时键入cast该对象。所以我想有像:

  def get_commercial_pack():
return CommercialPack(get_pack $ b

然后你有你所需要的一切,没有麻烦。因为我希望我的CommercialPack对象具有Pack对象具有的所有属性和功能。我只是想把它包装在我的新类。基本上我不想这样做:

 类CommercialPack(object):
def __init __ ,pack):
self.name = pack.name
self.weight = pack.weight
...

OR

  class CommercialPack(object):
def __init __ self,pack):
self.pack = pack



非常感谢。

解决方案

可能类似这样

  $ b def __init __(self,pack):
self .__ dict __。update(pack .__ dict__)

如果你不介意在包和商业包之间共享状态,你甚至可以这样做。

  class CommercialPack对象):
def __init __(self,pack):
self .__ dict__ = pack .__ dict__

在你的例子中应该是确定的,因为你不保存对包对象的任何其他引用



例如



<$ c $ p> PER_KM_PRICE = 100

class Pack(object):
def __init __(self,name,weight):
self.name = name
self.weight = weight


class CommercialPack(Pack):
def __init __(self,pack):
self .__ dict__ = pack .__ dict__

def get_delivery_price(self,distance):
return self.weight * PER_KM_PRICE * distance

def get_pack():
return bmp(pack,20)

cp = CommercialPack(get_pack())
print cp.get_delivery_price(3)
pre>

Let's say there is a library function called get_pack() which returns a Pack object:

class Pack(object):
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight
        ...

What I want to do is to wrap this object into my object which is let's say CommercialPack which has a couple of useful functions:

class CommercialPack(Pack):
    def get_delivery_price(self, distance):
        return self.weight * PER_KM_PRICE * distance
    ...

And then create a function which returns CommercialPack instead of Pack. In some other languages like Delphi or Java, you can type cast that object on the fly. So I want to have something like:

def get_commercial_pack():
    return CommercialPack(get_pack())

and then you have all you need with no hassle. Because I would like my CommercialPack object to have all the properties and functions that Pack object has. I just want to wrap it inside my new class. Basically I don't want to do something like this:

class CommercialPack(object):
    def __init__(self, pack):
        self.name = pack.name
        self.weight = pack.weight
        ...

OR

class CommercialPack(object):
    def __init__(self, pack):
        self.pack = pack

I'm looking for an elegant solution instead like I said, some kind of type casting or whatever I can do elegantly in Python.

Thanks very much.

解决方案

Perhaps something like this

class CommercialPack(object):
    def __init__(self, pack):
        self.__dict__.update(pack.__dict__)

You can even do this if you don't mind sharing state between the pack and the commercial pack

class CommercialPack(object):
    def __init__(self, pack):
        self.__dict__ = pack.__dict__

It should be ok in your example since you aren't keeping any other references to the pack object

eg.

PER_KM_PRICE = 100

class Pack(object):
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight


class CommercialPack(Pack):
    def __init__(self, pack):
        self.__dict__ = pack.__dict__

    def get_delivery_price(self, distance):
        return self.weight * PER_KM_PRICE * distance

def get_pack():
    return Pack("pack", 20)

cp = CommercialPack(get_pack())
print cp.get_delivery_price(3)

这篇关于Python中的对象类型转换(设计建议)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆